jQuery:引用和操作动态添加元素的父元素
我正在文档中加载一些 XML 并向其中添加一个 DIV 按钮。通过单击按钮,我不仅不想 fadeOut()
按钮,而且还不想淡出其父元素。虽然引用和淡出按钮本身正在工作,但我无法访问其父元素。 (请参阅我的代码中的最后几行)。
$(document).ready(function(){
var searchstring = decodeURIComponent(getUrlVars()["search"]);
$.ajax({
url: 'data.xml',
type: 'GET',
dataType: 'xml',
timeout: 10000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
$(xml).find('entry').filter(function(index) { return $('lemma', this).text() == searchstring; } ).each(function() {
$(this).find('part_gp > part').each(function(){
var more = $('<div class="more">+</div>');
$(this).after(more);
more.click(fader);
});
$( "#result" ).append( $(this) );
});
}
});
});
function fader() {
$(this).fadeOut(); // this works!!
$(this).parent().fadeOut(); // this doesn't work!!
}
更新:
XML/HTML 将如下所示:
<part_gp>
<part>Grafschaft</part>
<div class="more">+</div>
<pos> feminin / weiblich</pos>
</part_gp>
我想隐藏整个part_gp..
$(this).parents("part_gp").fadeOut();
也不起作用..
I'm loading some XML in my document and add a DIV-button to it. By clicking the button, I wan't to fadeOut()
not only the button, but also its parent element. While referencing and fading out the button itself is working, I fail to get access to its parent-element. (see the last lines in my code).
$(document).ready(function(){
var searchstring = decodeURIComponent(getUrlVars()["search"]);
$.ajax({
url: 'data.xml',
type: 'GET',
dataType: 'xml',
timeout: 10000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
$(xml).find('entry').filter(function(index) { return $('lemma', this).text() == searchstring; } ).each(function() {
$(this).find('part_gp > part').each(function(){
var more = $('<div class="more">+</div>');
$(this).after(more);
more.click(fader);
});
$( "#result" ).append( $(this) );
});
}
});
});
function fader() {
$(this).fadeOut(); // this works!!
$(this).parent().fadeOut(); // this doesn't work!!
}
UPDATE:
The XML/HTML will look like this:
<part_gp>
<part>Grafschaft</part>
<div class="more">+</div>
<pos> feminin / weiblich</pos>
</part_gp>
I want to hide the whole part_gp..
$(this).parents("part_gp").fadeOut();
didn't work either..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在使用
$(this).after()
。这就是将按钮放在要隐藏的块之后,而不是在其内部。你真正想要的是$(this).append()
。http://api.jquery.com/after/
http://api.jquery.com/append/
The problem is that you're using
$(this).after()
. That is putting your button after the block you want to hide, not inside of it. What you really want is$(this).append()
.http://api.jquery.com/after/
http://api.jquery.com/append/
我不知道标记是什么样子,但你可以尝试
i dont know how the markup looks like but you can try