的index.html的document.getElementById返回NULL
<body>
<script src="js/event_handler.js"></script>
<div id="some_wrapper"></div>
</body>
event_handler.js
$(document).ready(function() {
...
$('#some_wrapper').append (someHTMLcode);
foo(someTagIdIntoTheNewHtmlCode);
}
//Search if the tag id exist
function foo(tag_id){
if(document.getElementById(tag_id) == null){
console.log('#' + tag_id + " not found");
return;
}
console.log('#' + tag_id + " found");
}
当我追加到#some_wrapper的HTML代码,一切都显示成功,但如果我要处理的代码,例如。 document.getElementById(tag)返回总是为空。 为什么?
当你使用'document.getElementById'时'''''''document.getElementById(tag)'... S ee:https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById – 2014-11-21 15:25:31
在相关说明中,如果您使用的是jQuery,那么为什么要执行'document.getElementById'? – j08691 2014-11-21 15:26:31
在相关(更有帮助)的笔记上,您可以使用内置的JQuery方法遍历DOM [''''''''''](http://api.jquery.com/find/)。 – theWanderer4865 2014-11-21 15:28:02