我有以下在 ASP.NET Repeater 中呈现的 html:
<div class="divDE-item" onclick="addFilter(this);">
<span class="spnKey">Some key</span>
<div>1234</div>
</div>
我意识到在外部 div 上进行 onclick 可能不是最优雅的以 jQuery 为中心的方法。不过,考虑到我的情况,它效果很好。
这是我的 addFilter() 函数:
function addFilter(oDiv) {
$(document).ready(function() {
// Get and set the prefix text for the label. i.e. "Key = "
sDEName = $(oDiv).find("span").text();
$('#<%= lblDEName.ClientID %>').text(sDEName + " = ");
// Get the actual filter text value. i.e. "1234"
// sFilter = $(oDiv).text();
var sFilter = $(oDiv).filter(function() {
var filtered = $(this).not(".spnKey");
return filtered
});
$('#<%= txtValue.ClientID %>').val(sFilter);
});
}
目标是输出如下所示(1234 是文本框的值):
某个键 = 1234
但是,我得到的输出是:Some key = Some key 1234
最佳答案
您不需要调用过滤器
。
您可以简单地编写$('div:not(.spnKey)', oDiv).text()
。
要回答您的问题,您使用的过滤器
并且没有
错误。 filter
方法返回一个 jQuery 对象,其中包含您调用它的对象中的一些元素。 (不是他们的 child )。要以这种方式使用它,您应该调用 $(oDiv).children().filter(...)
。
not
方法与 filter
相反,它返回 jQuery 对象中与函数选择器不匹配的元素。因此,您也可以编写 $(oDiv).children().not('.spnKey')
。
关于jquery - 如何使用 jQuery filter() 获取特定的 div 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2140416/