Javascript / jQuery 查找和替换
Javascript / jQuery Find & Replace
我想找到:
<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal();">
并将其替换为:
<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal(); applyCode();">
。
简而言之,将applyCode();
插入onclick
。
我认为查找和替换不是您最好使用的工具。相反,您可以使用 Javascript 为您附加事件。当您用 jQuery 标记问题时,试试这个:
<input type="button" class=" btn btn-lg btn-info" value="Continue">
$(function() {
$('input[type=button]').click(function() {
loadFinal();
applyCode();
});
});
请注意,上面的选择器纯粹是为了演示目的,您最好设置一个 id
属性,或者在元素上设置一个特定的 class
并通过它来选择它。您甚至可以连接到表单的 submit
事件,假设此按钮用于该目的。
您可以做 2 件事。方法 1 会从按钮中删除所有点击事件并添加事件,方法 2 会在现有事件之外添加其他事件。
方法一
$('input:button').unbind('click'); // remove all click events associated
$('input:button').on("click",function() // add new click events
{
loadFinal();
applyCode();
});
方法二
$('input:button').on("click",function() // add additional click events
{
applyCode();
});
更新:
为了将 applycode() 事件应用或附加到具有 loadfinal() 的同一元素,您可以使用 $._data
提取事件并检查某个事件是否存在。
$("input:button").click(function() {
var events = $._data(document.getElementById('btn1'), "events");
$.each(events,function(i,o)
{
alert(i);// give the event type like 'click'
alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
});
});
或者,您可以直接定位点击事件。
var events = $._data(document.getElementById('btn1'), "events").click;
$.each(events,function(i,o)
{
alert(i);// give the index
alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
});
});
我想找到:
<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal();">
并将其替换为:
<input type="button" class=" btn btn-lg btn-info" value="Continue" onclick="loadFinal(); applyCode();">
。
简而言之,将applyCode();
插入onclick
。
我认为查找和替换不是您最好使用的工具。相反,您可以使用 Javascript 为您附加事件。当您用 jQuery 标记问题时,试试这个:
<input type="button" class=" btn btn-lg btn-info" value="Continue">
$(function() {
$('input[type=button]').click(function() {
loadFinal();
applyCode();
});
});
请注意,上面的选择器纯粹是为了演示目的,您最好设置一个 id
属性,或者在元素上设置一个特定的 class
并通过它来选择它。您甚至可以连接到表单的 submit
事件,假设此按钮用于该目的。
您可以做 2 件事。方法 1 会从按钮中删除所有点击事件并添加事件,方法 2 会在现有事件之外添加其他事件。
方法一
$('input:button').unbind('click'); // remove all click events associated
$('input:button').on("click",function() // add new click events
{
loadFinal();
applyCode();
});
方法二
$('input:button').on("click",function() // add additional click events
{
applyCode();
});
更新:
为了将 applycode() 事件应用或附加到具有 loadfinal() 的同一元素,您可以使用 $._data
提取事件并检查某个事件是否存在。
$("input:button").click(function() {
var events = $._data(document.getElementById('btn1'), "events");
$.each(events,function(i,o)
{
alert(i);// give the event type like 'click'
alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
});
});
或者,您可以直接定位点击事件。
var events = $._data(document.getElementById('btn1'), "events").click;
$.each(events,function(i,o)
{
alert(i);// give the index
alert(o); // give the event object - you can extract the event name from the object and do the compare with the existint event.
});
});