一个选择器,其中包含该选择器和类选择器
这是我的问题的类比(一个选择器,该选择器具有该选择器和一个类选择器):
假设我选择任意 HTML 文档中的所有黄色(类)div 元素。我希望每个人都检查属性是否为 yes = 1。如果属性“yes”等于“1”,那么我希望类“blue”的孩子的属性“no”等于“1”;
$('div .yellow').each(function(){
if($(this).attr('yes') == 1){
$(this '.blue:first-child').attr('no', 1);//This line needs to be fixed
}
});
我知道 this.getElementsByClassName('blue')[0]
行解决了这个问题。但在我真正的问题(不是这个类比)中,我想使用 addClass 和 removeClass ,它们仅适用于 jQuery 对象。使用addClass和removeClass之外的其他函数比较麻烦。
更新:
这是我真正问题的代码片段。我在 javascript 中遇到了一些关于“this”的问题。
我想要一个邀请按钮,当我单击它时,类名可见。该按钮位于 className 为“box”的 div 元素内。我知道代码片段中的“this”有问题。但我希望按钮而不是框更改为可见,
$('.Box').each(function(){
if($(this).attr('hasButton') != 1){
var invite = document.createElement('div');
invite.className = 'invite invisible';
invite.innerHTML = 'invite';
$(this).attr('hasButton', 1);
this.appendChild(invite);
invite.addEventListener('mouseover', function(event){
$('.invite', this).removeClass('invisible');//this line is not functioning
$('.invite', this).addClass('visible');//neither this
}, false);
}
});
好的解决了。我传递了一个元素作为属性而不是 classSelector。
$(invite).removeClass('invisible');
$(invite).addClass('visible');
Here is an analogy of my problem(a selector which has this selector together with a class selector):
Let say that I selects all yellow(classes) div elements in a arbitrary HTML document. And I want each to check if the attribute is yes = 1. If the attribute 'yes' equals '1', then I want the child with class 'blue' have the attribute 'no' equals '1';
$('div .yellow').each(function(){
if($(this).attr('yes') == 1){
$(this '.blue:first-child').attr('no', 1);//This line needs to be fixed
}
});
I know that the line this.getElementsByClassName('blue')[0]
fixes this problem. But in my real problem (not this analogy) I want to use addClass and removeClass which only functions with jQuery objects. It is to cumbersome to use other functions than addClass and removeClass.
UPDATE:
Here is a code snippet from my real problem. I got some problem with "this" in javascript.
I want a invited button to have the className visible when I click on it. The button lies within a div element with className 'box'. I know that there are problem with 'this' on the code snippet. But I want the button and not the box to change to visible
$('.Box').each(function(){
if($(this).attr('hasButton') != 1){
var invite = document.createElement('div');
invite.className = 'invite invisible';
invite.innerHTML = 'invite';
$(this).attr('hasButton', 1);
this.appendChild(invite);
invite.addEventListener('mouseover', function(event){
$('.invite', this).removeClass('invisible');//this line is not functioning
$('.invite', this).addClass('visible');//neither this
}, false);
}
});
Ok solved it. I passed an element as attribute instead of classSelector.
$(invite).removeClass('invisible');
$(invite).addClass('visible');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的意思是每个蓝色
div
都位于其自己的黄色div
中吗?如果是这样,请尝试以下操作:这告诉 jQuery 选择仅在
this
范围内找到的.blue
元素(引用.yellow
元素)当前正在由each()
迭代)。Do you mean each blue
div
lives in its own yellowdiv
? If so, try this:This tells jQuery to select
.blue
elements only found within the scope ofthis
(which refers to the.yellow
element currently being iterated byeach()
).您可以使用
tagname[attrname=attrvalue]
来匹配任意属性。您还可以使用
find()
仅在特定元素内查找。You can use the
tagname[attrname=attrvalue]
to match arbitrary attributes.You can also use
find()
to only look within a specific element.