[英]jquery Replace DIV data and then track clicks on it
I have a <div id="myContainer"></div>
. 我有一个<div id="myContainer"></div>
。 I also have a button: <input type="button" value="Send" id="sendButton">
While clicking at the button: it replaces the DIV with another: 我还有一个按钮: <input type="button" value="Send" id="sendButton">
点击按钮时:它将DIV替换为另一个:
$( "#sendButton" ).click(function() {
$( "#myContainer" ).replaceWith("<div id='calc'><input type=\"text\" id=\"screen\" value=0><button id=\"add\">+</button><button id=\"mul\">*</button><button id=\"settings\">settings</button><button id=\"clear\">clear</button></div>");
});
I want to activate another function after click on a new button I've just put ( <button id=\\"mul\\">*</button>
): 我想在点击我刚刚放入的新按钮后激活另一个功能( <button id=\\"mul\\">*</button>
):
$( "#mul" ).click(function() {
console.log(' mul clicked!');
});
Which doesn't work, the fact that I'm waiting for a click on a div that just created have something to do with it? 哪个不起作用,我正在等待刚刚创建的div的点击与它有关?
You need to attach event to #mul
. 您需要将事件附加到#mul
。 Because it is appended dynamically, $("#mul").click()
will not work. 因为它是动态附加的, $("#mul").click()
将不起作用。
.on()
attaches event handlers to the currently selected set of elements. .on()
将事件处理程序附加到当前选定的元素集。
Try: 尝试:
$("body").on("click","#mul",function(){
console.log("mul clicked!");
});
When you call $( "#mul" ).click()
, you're attaching an event handler to #mul
as it exists at that point. 当你调用$( "#mul" ).click()
,你将事件处理程序附加到#mul
因为它在那时存在。 To fix this, just call $( "#mul" ).click()
after you create #mul
. 要解决此问题,请在创建#mul
后调用$( "#mul" ).click()
。 #mul
$( "#mul" ).click()
。
$( "#sendButton" ).click(function() {
$( "#myContainer" ).replaceWith("<div id='calc'><input type=\"text\" id=\"screen\" value=0><button id=\"add\">+</button><button id=\"mul\">*</button><button id=\"settings\">settings</button><button id=\"clear\">clear</button></div>");
$( "#mul" ).click(function() {
console.log( ' mul clicked!' );
});
});
You could also use jQuery's .on
method with the optional selector, called a delegated event handler according to the documentation. 您还可以将jQuery的.on
方法与可选的选择器一起使用,根据文档称为委托事件处理程序 。 Take a look at the API for jQuery if that's what you want: jQuery API documentation . 如果你想要的话,请查看jQuery的API : jQuery API文档 。 The basic usage would be something like 基本用法就像是
$( document ).on( "click", "#mul", function( ) {
console.log( ' mul clicked!' );
});
use this 用这个
$(document).on("click","#mul",function() {
instead of 代替
$( "#mul" ).click(function() {
or 要么
$( "#sendButton" ).click(function() {
$( "#myContainer" ).replaceWith("<div id='calc'><input type=\"text\" id=\"screen\" value=0><button id=\"add\">+</button><button id=\"mul\">*</button><button id=\"settings\">settings</button><button id=\"clear\">clear</button></div>");
$( "#mul" ).click(function() {
console.log(' mul clicked!');
});
});
$( "#sendButton" ).click(function() {
$( "#myContainer" ).replaceWith("<div id='calc'><input type=\"text\" id=\"screen\" value=0><button id=\"add\">+</button><button id=\"mul\">*</button><button id=\"settings\">settings</button><button id=\"clear\">clear</button></div>");
// add listener here
$( "#mul" ).click(function() {
console.log(' mul clicked!');
});
$( "#mul" ).trigger("click"); // add this to your code
});
http://api.jquery.com/trigger/ see detail of trigger() http://api.jquery.com/trigger/查看触发器的详细信息()
http://jsfiddle.net/tnnj5/ here is a demo http://jsfiddle.net/tnnj5/这是一个演示
you must add listener after the new content has insert into dom 在新内容插入dom后必须添加侦听器
You can use .live()
method to bind event with dynamically added content. 您可以使用.live()
方法将事件与动态添加的内容绑定。
Try this: 尝试这个:
$("#mul").live("click", function() {
console.log(' mul clicked!');
});
You can also use jquery .on()
, But here you add dynamic content. 您也可以使用jquery .on()
,但是在这里添加动态内容。 So you need to use event delegation to register the event handler like: 所以你需要使用事件委托来注册事件处理程序,如:
$(document).on("click","body #mul", function() {
console.log(' mul clicked!');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.