问题
I have a div whose content gets dynamically generated from innerHTML and I cant seem to target those elements using jQuery for validation, Eg This is the page:
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
</head>
<body>
<span id="alert1">Alert1</span>
<input type="button" value="Click Me" id="button1" />
<div id="empty"></div>
</body>
</html>
and this is the js code:
<script type = "text/javascript">
$(document).ready(function() {
$('#button1').click(function() {
var html = "uname <input type='text' id='uname'><br />password <input type='text' id='password'><br /><span id='alert'>Alert</span>";
$('#empty').html(html);
});
$('#alert').click(function() {
alert('Alert');
});
$('#alert1').click(function() {
alert('Alert1');
});
});
</script>
The alert for the span with id alert1 works but for id alert which is set using innerHTML or .html() does not work so how to fix this?
Thank you in advance
回答1:
The problem here is that when you write like this..
$("#element").click(function() {
alert("Alert!");
});
.. it will only create events handlers for elements that exist when you execute the code. It won't create event handlers for future elements even if they have the same jQuery selector ("#element" in this case).
What you need to do is bind with .on(event, selector, function)
on a parent element, as the events "bubble up" the hierarchy. That way you can handle future events from the specified selector. Personally I would do it like this:
$(document).ready(function() {
$('#button1').click(function() {
var html = "<insert lots of html stuff here>";
$('#empty').html(html);
});
$(document).on("click", "#alert", function() {
alert("Alert!");
});
$(document).on("click", "#alert1", function() {
alert("Alert1!");
});
});
回答2:
you should use delegate
$(elements).delegate(selector, events, data, handler);
as per your code
$(document).ready(function() {
$("yourdiv").delegate("#alert, #button, #Alert1", "click", function() {
alert('Alert');
});
});
回答3:
Use event delegation to bind click handler, use .on() for this
$('#empty').on('click', '#alert1', function() {
alert('Alert1');
});
or you could just bind the handler after you create the content.
回答4:
Use .on()
instead of .click()
to bind events at run-time. You may try .live()
but it is deprecated.
Here's the fiddle : http://jsfiddle.net/Akyrk/
回答5:
you have to attach the listeners AFTHER you added the objects to the dom!
so afther .html(...)
<script type = "text/javascript">
$(document).ready(function() {
$('#button1').click(function() {
var html = "uname <input type='text' id='uname'><br />password <input type='text' id='password'><br /><span id='alert'>Alert</span>";
$('#empty').html(html);
$('#alert').click(function() {
alert('Alert');
});
});
});
.on()
is also an option. You bind listener independent if they are renderd to the dom or not. But I recommend the first approach. If you are writing larger applications the approach of attaching handlers after creation is much clearer then event delegation automatically attaching event handlers maybe without you even noticing. for 1 or 2 it's ok but if you're adding multiple events to multiple objects (tags, classes) you could loose oversight...
来源:https://stackoverflow.com/questions/11716713/how-to-target-elements-set-from-innerhtml-or-html-using-jquery