I have an array of names, which i need to see if they match names in a DIV:
我有一个名字数组,我需要看看他们是否匹配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 === item){
alert("do somethign");
}
}
I have a bunch of DIV.name classes and if the name matches the topicName array, i need to do something;
我有一堆DIV.name类,如果名称与topicName数组匹配,我需要做一些事情;
4 个解决方案
#1
0
HTML:
<div class="name">jen</div>
<div class="name">bob</div>
<div class="name">sally</div>
jQuery:
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");
}
});
#2
1
$("DIV.grid_6 DIV.name").each(function(i, item){
if ( $.inArray( item, topicName ) !== -1 ){
alert("do somethign");
}
}
#3
0
Instead of
if (topicName === item)
do this, if the text to match is the content of the div
:
这样做,如果要匹配的文本是div的内容:
if ($.inArray($(item).text(), topicName) != -1)
or, if it's in an attribute (such as <div name="foo">
):
或者,如果它在属性中(例如
if ($.inArray($(item).attr('name'), topicName) != -1)
#4
0
you can use some kind of tricky way like this;
你可以使用这种棘手的方式;
$(document).ready(function(){
var a = ['foo', 'bar', 'c'];
//
// $('.' + a.join(', .')) => $('.foo, .bar, .c')
//
$('.' + a.join(', .')).mouseover(function() {
console.log($(this).attr('class'));
});
});
you can simply select them and do what ever you want. If the name is not exists, there will be no binding. It means no problem at all.
你可以简单地选择它们,做任何你想做的事。如果名称不存在,则不会绑定。这意味着没有任何问题。
#1
0
HTML:
<div class="name">jen</div>
<div class="name">bob</div>
<div class="name">sally</div>
jQuery:
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");
}
});
#2
1
$("DIV.grid_6 DIV.name").each(function(i, item){
if ( $.inArray( item, topicName ) !== -1 ){
alert("do somethign");
}
}
#3
0
Instead of
if (topicName === item)
do this, if the text to match is the content of the div
:
这样做,如果要匹配的文本是div的内容:
if ($.inArray($(item).text(), topicName) != -1)
or, if it's in an attribute (such as <div name="foo">
):
或者,如果它在属性中(例如
if ($.inArray($(item).attr('name'), topicName) != -1)
#4
0
you can use some kind of tricky way like this;
你可以使用这种棘手的方式;
$(document).ready(function(){
var a = ['foo', 'bar', 'c'];
//
// $('.' + a.join(', .')) => $('.foo, .bar, .c')
//
$('.' + a.join(', .')).mouseover(function() {
console.log($(this).attr('class'));
});
});
you can simply select them and do what ever you want. If the name is not exists, there will be no binding. It means no problem at all.
你可以简单地选择它们,做任何你想做的事。如果名称不存在,则不会绑定。这意味着没有任何问题。