<c:forEach items="${list}" var="item">
<div class="block" id="${item.id}" data-num="${item.itemRead}">
<a id="${item.id}"
href="javascript:showFeedItem('${item.id}','${item.itemRead}');">
</div>
</c:forEach>
Why do I have undefinded attribute name data-num when creating a new data attribute data-num
? jQuery:
为什么在创建新的数据属性data-num时,我有未定义的属性名称data-num? jQuery的:
$(document).ready(function(){
$('.block').each(function(i, obj) {
if($(this).attr("data-num")=="0"){
//need to style a inside this
$(this).css("background-color","black");
}
});
});
How to target a
like this:
如何定位这样的:
$('.block[id="' + id + '"] a').css('color', 'black');
but using $(this).css("background-color","black");
?
但使用$(this).css(“background-color”,“black”);?
3 个解决方案
#1
2
Pass this
as the context to jQuery selector
将此作为上下文传递给jQuery选择器
$(document).ready(function () {
$('.block').each(function (i, obj) {
if ($(this).attr("data-num") == "0") {
//need to style a inside this
$('a', this).css("background-color", "black");
//or $(this).find('a').css("background-color", "black");
}
});
});
You can also shorten this to
你也可以缩短它
$(document).ready(function () {
$('.block[data-num="0"] a').css("background-color", "black");
});
#2
0
If you want grab direct descendant of this
(like it is in your code), you can use .children() method.
如果你想抓住这个的直接后代(就像它在你的代码中),你可以使用.children()方法。
$(this).children(a).css("background-color","black");
#3
0
I guess you need to change this:
我想你需要改变这个:
<c:forEach items="${list}" var="item">
<div class="block" id="${item.id}" data-num="${item.itemRead}">
<a href="javascript:showFeedItem('${item.id}','${item.itemRead}');">
</div>
</c:forEach>
removed id="${item.id}"
from the anchor. What seemed to me that you are adding same ids to different items in a single page but it has to be unique for each element.
从锚中删除了id =“$ {item.id}”。在我看来,您在一个页面中为不同的项目添加相同的ID,但每个元素必须是唯一的。
Now in the js you have to use .find()
to style the anchor and style it:
现在在js中你必须使用.find()来设置样式并设置样式:
$(document).ready(function(){
$('.block').each(function(i, obj) {
if($(this).attr("data-num")=="0"){ // or if($(this).data("num")=="0"){
$(this).find('a').css("background","black");
}
});
});
#1
2
Pass this
as the context to jQuery selector
将此作为上下文传递给jQuery选择器
$(document).ready(function () {
$('.block').each(function (i, obj) {
if ($(this).attr("data-num") == "0") {
//need to style a inside this
$('a', this).css("background-color", "black");
//or $(this).find('a').css("background-color", "black");
}
});
});
You can also shorten this to
你也可以缩短它
$(document).ready(function () {
$('.block[data-num="0"] a').css("background-color", "black");
});
#2
0
If you want grab direct descendant of this
(like it is in your code), you can use .children() method.
如果你想抓住这个的直接后代(就像它在你的代码中),你可以使用.children()方法。
$(this).children(a).css("background-color","black");
#3
0
I guess you need to change this:
我想你需要改变这个:
<c:forEach items="${list}" var="item">
<div class="block" id="${item.id}" data-num="${item.itemRead}">
<a href="javascript:showFeedItem('${item.id}','${item.itemRead}');">
</div>
</c:forEach>
removed id="${item.id}"
from the anchor. What seemed to me that you are adding same ids to different items in a single page but it has to be unique for each element.
从锚中删除了id =“$ {item.id}”。在我看来,您在一个页面中为不同的项目添加相同的ID,但每个元素必须是唯一的。
Now in the js you have to use .find()
to style the anchor and style it:
现在在js中你必须使用.find()来设置样式并设置样式:
$(document).ready(function(){
$('.block').each(function(i, obj) {
if($(this).attr("data-num")=="0"){ // or if($(this).data("num")=="0"){
$(this).find('a').css("background","black");
}
});
});