Jquery 将数组与DIV中的文本匹配
我有一个名称数组,我需要查看它们是否与DIV中的名称匹配:Jquery 将数组与DIV中的文本匹配,jquery,arrays,Jquery,Arrays,我有一个名称数组,我需要查看它们是否与DIV中的名称匹配: var topicName = []; $.each(top3, function(i, item) { if (item.featured === false) { topicName.push(item.username); } }); $("DIV.grid_6 DIV.name").each(function(i, item){ if (topicName === ite
var topicName = [];
$.each(top3, function(i, item) {
if (item.featured === false) {
topicName.push(item.username);
}
});
$("DIV.grid_6 DIV.name").each(function(i, item){
if (topicName === item){
alert("do somethign");
}
}
我有一堆DIV.name类,如果名称与topicName数组匹配,我需要做一些事情 而不是
$("DIV.grid_6 DIV.name").each(function(i, item){
if ( $.inArray( item, topicName ) !== -1 ){
alert("do somethign");
}
}
if (topicName === item)
如果要匹配的文本是div的内容,请执行此操作:
if ($.inArray($(item).text(), topicName) != -1)
或者,如果它位于属性中(例如
):
HTML:
你可以用这样一种巧妙的方法
$(document).ready(function(){
var a = ['foo', 'bar', 'c'];
//
// $('.' + a.join(', .')) => $('.foo, .bar, .c')
//
$('.' + a.join(', .')).mouseover(function() {
console.log($(this).attr('class'));
});
});
你可以简单地选择它们,做任何你想做的事情。如果名称不存在,则不会有绑定。这意味着一点问题都没有。你的第二个if
语句看起来很痛苦。请修复您的()
和{}
如果(topicName==item){}
是什么?如果topciName
是一个数组,则不能仅比较这样的值。
<div class="name">jen</div>
<div class="name">bob</div>
<div class="name">sally</div>
var topicName = ["jen","bob","michelle"];
$("div.name").each(function(){
var nameFound = $.inArray($(this).text(), topicName);
if (nameFound === -1){
alert($(this).text() + " not found in array");
} else {
alert($(this).text() + " found in array");
}
});
$(document).ready(function(){
var a = ['foo', 'bar', 'c'];
//
// $('.' + a.join(', .')) => $('.foo, .bar, .c')
//
$('.' + a.join(', .')).mouseover(function() {
console.log($(this).attr('class'));
});
});