admin管理员组文章数量:1321447
I'm appending some HTML containing javascript.
<td onclick="toggleDay('+data+',this,\'tue\');">T</td>
and
<img src="img/cross.png" onclick="deleteAlarm('+data+');">
These two pieces of code are in the big amount of HTML I'm appending.
They work fine if they are already there when the page loads but not when I'm appending.
What do you suggest me to do? Is it needed some sort request for the DOM to re-interpret the JavaScript after the append or?
EDIT:
Just some more info, I'm with this problem, because I'm adding some stuff with AJAX to the database, and on success I'm appending the html to where it needs to be. Kinda the same way SO does with the ments.
Edit2:
Even with all the discussion about it, thanks for the answers, got it working.
I'm appending some HTML containing javascript.
<td onclick="toggleDay('+data+',this,\'tue\');">T</td>
and
<img src="img/cross.png" onclick="deleteAlarm('+data+');">
These two pieces of code are in the big amount of HTML I'm appending.
They work fine if they are already there when the page loads but not when I'm appending.
What do you suggest me to do? Is it needed some sort request for the DOM to re-interpret the JavaScript after the append or?
EDIT:
Just some more info, I'm with this problem, because I'm adding some stuff with AJAX to the database, and on success I'm appending the html to where it needs to be. Kinda the same way SO does with the ments.
Edit2:
Even with all the discussion about it, thanks for the answers, got it working.
Share Improve this question edited Sep 20, 2018 at 5:45 fabrik 14.4k8 gold badges57 silver badges71 bronze badges asked Mar 3, 2009 at 20:28 fmsffmsf 37.2k49 gold badges153 silver badges196 bronze badges 05 Answers
Reset to default 4I'd do it like this:
First add id attributes to your html:
<td id="toggleDayCell">T</td>
<img src="img/cross.png" id="crossImg">
Then have Javascript that runs on the onload event of the page and attaches to the events you're interested in.
In PrototypeJS, it might look like this:
Event.observe(window, 'load', function(){
if( $('toggleDayCell') ) {
Event.observe( $('toggleDayCell'), 'click', function(event){
//do stuff here
}
}
if( $('crossImg') ) {
Event.observe( $('crossImg'), 'click', function(event) {
//do stuff here
}
}
});
Using something like Prototype (or jQuery) is nice because it takes care of cross-browser patibility.
Are you appending the HTML via AJAX? If so you will have to manually eval() the JavaScript that you are returning. You could wrap the response in a div and do something like this:
wrapper.getElementsByTagName("script")
// for script in wrapper...
eval(script.innerHTML)
If you are using a library like prototype it would be much simpler as you can pass the response to the evalScripts() method.
Append the HTML to the document then programatically add the onclick event to the object.
This is off the top of my head but... I think you can do it this way:
IE:
Object.onclick = someFuntion
FF:
Object.addEventListener('click', someFunction);
AJAX has some quirks... especially with IE. Example: you cannot use the innerHTML property to insert/update anything in a table element (in IE). It's better to use the DOM functions to insert/update/delete element nodes (or add event handlers). You can use the eval() function mentioned to run dynamically loaded javascript that modifies the document using DOM objects.
You should be using addEventListener('click', /*...*/)
instead of setting onclick in innerHTML or whatever.
IE calls addEventListener
something else entirely, so you'll have to pensate for that too.
(Edit: IE calls it attachEvent()
. Hope this is of some use.)
本文标签: Appended HTML that contains javascript doesn39t runhow can I make it runStack Overflow
版权声明:本文标题:Appended HTML that contains javascript doesn't run, how can I make it run? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742100106a2420764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论