[英]jquery can not find div element per ID
Ok, I'm not exactly a js newbie but this is just bizarre. 好吧,我不是一个新手,但这只是奇怪的。
I have a wordpress here and want to add a comment box, which should load comments per ajax if the box is visible, when the user scrolls down to that box, so I can save performance and don't need to load comments every time someone just requests the page. 我在这里有一个wordpress,想要添加一个评论框,如果框是可见的,应该加载评论每个ajax,当用户向下滚动到该框时,所以我可以保存性能,不需要每次有人加载评论只是请求页面。
To accomplish this, I added the following snippet to my wp single.php post template: 为此,我将以下代码段添加到我的wp single.php帖子模板中:
<?php /** Begin Post Content **/ ?>
<?php the_content(); ?>
<?php wp_link_pages('before=<div class="pagination">'._r('Pages:').'&after=</div><br />'); ?>
<div id="vbcomments_box" style="display:visible">asd</div>
<?php print vb_comments_ajax($post); ?>
<?php /** Begin Tags **/ ?>
the 4th and 5th line is from me, it introduces a div#vbcomments_box with an id and random content for now. 第4行和第5行来自我,它引入了一个带有id和随机内容的div#vbcomments_box。
After that, I call a function to inject javascript code into the page (i know this can be fancier, but for development purposes this will stay for now.) : 之后,我调用一个函数将javascript代码注入到页面中(我知道这可能会更加漂亮,但出于开发目的,这将暂时保留。):
<script>
(function(){window.setInterval(function() {
console.log('the element is: ', $('#vbcomments_box'));
if($('#vbcomments_box').is(':visible')){
vbc_is_visible();
}
}, 1000);
// Function / event that will be started and stopped
function vbc_is_visible() {
alert('HALLO');
$("#vbcomments_box").html("LOADING...");
}
console.log("Inside script");})();
</script>
This just sets a timer which tests for the selector $('#vbcomments_box').is(':visible') 这只是设置一个计时器来测试选择器$('#vbcomments_box')。是(':visible')
What happens is, that $('#vbcomments_box') is always null. 会发生什么,$('#vbcomments_box')始终为空。 I tested everything.
我测试了一切。 I changed vbcomments_box to a class, i added div before the #, I tried looking it up per document.getElementById (which is found fortunately, but I can't do anything with it)
我把vbcomments_box改成了一个类,我在#之前添加了div,我试着按照document.getElementById查找它(幸运的是发现了,但我无法用它做任何事情)
So why can't jquery find this simple div element where I'm 10000% sure that it exists in the sourcecode and the js stuff gets called AFTER the div has been placed into the sourcecode? 那么为什么jquery不能找到这个简单的div元素,我10000%确定它存在于源代码中,并且在将div放入源代码后调用js的东西?
What am I doing wrong? 我究竟做错了什么?
console says: 控制台说:
the element is: null (index):611
Uncaught TypeError: Cannot call method 'is' of null
Try this: 尝试这个:
<?php the_content(); ?>
<?php wp_link_pages('before=<div class="pagination">'._r('Pages:').'&after=</div><br />'); ?>
<div id="vbcomments_box" style="display:visible;">asd</div>
<?php print vb_comments_ajax($post); ?>
<?php /** Begin Tags **/ ?>
...... ......
<script>
(function(){window.setInterval(function() {
console.log('the element is: ', jQuery('#vbcomments_box'));
if(jQuery('#vbcomments_box').is(':visible')){
vbc_is_visible();
}
}, 1000);
// Function / event that will be started and stopped
function vbc_is_visible() {
alert('HALLO');
jQuery("#vbcomments_box").html("LOADING...");
}
console.log("Inside script");})();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.