我必须隐藏我的 parent span
就在<a>
之上标记并显示 span
就在它下面。(在 <a>
标签上单击它时)。
我需要使用 getParent() 因为它们是另一个相同的 <div>
重复
我的 HTML 是
<div class="Test">
<p class="commentBody">
<span class="view_more-comment">abcd...
<a class="view_more_link" href="javascript:void(0);" onclick="$(this).getParent().getNext().style.display='';$(this).getParent().style.display='none';">more</a>
</span>
<span class="view_more" style="display: none;">abcdefghijklmnopqrstuvwzyz...
<a class="view_less_link" onclick="$(this).getParent().getPrevious().style.display='';$(this).getParent().style.display='none';">less</a>
</span>
</p>
</div>
当我使用这个 console
显示错误
Uncaught TypeError: $(...).getParent is not a function
有什么方法可以解决这个问题吗?
最佳答案
jQuery
中没有getParent()
。使用:
onclick="$(this).parent().prev().show(); $(this).parent().hide();"
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
我建议您使用 on
进行事件处理 而不是内联处理程序
编辑
使用 jQuery:
$('.view_less_link').on('click', function() {
$(this).closest('.commentBody').find('.view_more-comment').show();
$(this).parent().hide();
});
关于Javascript "getParent() "不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749961/