$(this) children 总是 returns undefined
$(this) children always returns undefined
我对 undefined
被 innerHTML
编辑 return 有疑问。也许是因为 html()
和 innerHTML
之间的差异?
我的JavaScript代码:
var content2 = document.getElementById("content2");
var opis;
$(function() {
$('.blok').click(function(e) {
opis = $(this).next('.opis');
if (!$('.popup:visible').length) {
content2.innerHTML= opis.html();
$('.popup').fadeIn();
}
e.preventDefault();
return false;
});
$('.popup .close, .popup .bg').click(function() {
$(this).parents('.popup').fadeOut();
});
})
我的HTML:
<body oncontextmenu="return false" onselectstart="return false" onselect="return false" oncopy="return false">
<div class="popup">
<div class="bg"></div>
<div class="container">
<div id="content2"> </div>
</div>
</div>
And example of "blok":
<div class="blok"> $$|DO| = |OC| = 5$$ <div class="opis">2</div>
</div>
为什么 content2 总是 return undefined
?
具有 class blok
和 opis
的元素在 DOM 结构中不在同一级别。所以 $(this).next('.opis')
是无效的。 jquery
next()
当元素处于同一 DOM 级别时使用。您需要以下表达式才能找到正确的元素。 opis
实际上是 block
的 child
$('.blok').click(function(e) {
opis = $(this).find('.opis');
示例:https://jsfiddle.net/DinoMyte/pmnsvzrr/5/
或
$('.blok').click(function(e) {
$(this).children('.opis');
我对 undefined
被 innerHTML
编辑 return 有疑问。也许是因为 html()
和 innerHTML
之间的差异?
我的JavaScript代码:
var content2 = document.getElementById("content2");
var opis;
$(function() {
$('.blok').click(function(e) {
opis = $(this).next('.opis');
if (!$('.popup:visible').length) {
content2.innerHTML= opis.html();
$('.popup').fadeIn();
}
e.preventDefault();
return false;
});
$('.popup .close, .popup .bg').click(function() {
$(this).parents('.popup').fadeOut();
});
})
我的HTML:
<body oncontextmenu="return false" onselectstart="return false" onselect="return false" oncopy="return false">
<div class="popup">
<div class="bg"></div>
<div class="container">
<div id="content2"> </div>
</div>
</div>
And example of "blok":
<div class="blok"> $$|DO| = |OC| = 5$$ <div class="opis">2</div>
</div>
为什么 content2 总是 return undefined
?
具有 class blok
和 opis
的元素在 DOM 结构中不在同一级别。所以 $(this).next('.opis')
是无效的。 jquery
next()
当元素处于同一 DOM 级别时使用。您需要以下表达式才能找到正确的元素。 opis
实际上是 block
$('.blok').click(function(e) {
opis = $(this).find('.opis');
示例:https://jsfiddle.net/DinoMyte/pmnsvzrr/5/
或
$('.blok').click(function(e) {
$(this).children('.opis');