在 jquery 中找到将 css 应用于所有跨度
我正在 asp.net mvc 中借助 js 和 jquery 制作一棵树。
有一个添加按钮,可以添加兄弟姐妹和同级孩子。
为了确定要做什么,我使用以下代码。
//to check from where the function is called
var checkClass = $('#UlPrnt').find('span').css('background-color', 'Lime').length;
if (checkClass == 0) {
AddSiblings();
$('#hdnChkSibbling').val('2');
}
else {
debugger
var getValue = $('#dvTree').find('span').css('background-color', 'Lime');
var spnID = getValue[1].id;
var check = spnID.indexOf("spn");
if (check>0) {
AddSiblings();
$('#'+spnID).css('background-color', '');
}
else {
//call the function to append the same level child
}
}
当我在jquery中使用find函数时,我的解释是它将返回dom的编号,其中相应的bg颜色是石灰。
但它的作用是将 bgcolor 应用于所有 span 。
如何获取bgcolor为石灰的span的id。
每件事都是动态创建的(span,div)只是想添加以获得更好的图片。
i am making a tree with the help of js and jquery in asp.net mvc.
there is a add button which add the sibling and same level child .
to identify what is to be done i am using the following code.
//to check from where the function is called
var checkClass = $('#UlPrnt').find('span').css('background-color', 'Lime').length;
if (checkClass == 0) {
AddSiblings();
$('#hdnChkSibbling').val('2');
}
else {
debugger
var getValue = $('#dvTree').find('span').css('background-color', 'Lime');
var spnID = getValue[1].id;
var check = spnID.indexOf("spn");
if (check>0) {
AddSiblings();
$('#'+spnID).css('background-color', '');
}
else {
//call the function to append the same level child
}
}
when i was going through the find function in jquery what i interpreted is that it will return the no of dom where the corresponding bg color is lime.
but what it does it applys the bgcolor to all the span .
how to get the ids of the span whose bgcolor is lime.
every thing is created dynamically (span ,div) just wanted to add for getting a better picture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您错误地使用了 jQuery .css() 方法。您可以使用 .css() 来获取或设置 css 属性。有关更多详细信息,请参阅:http://api.jquery.com/css/。
您应该向所有想要变成石灰色的元素添加一个类,而不是使用 css:
然后,在 css 文件中,指定石灰色类的样式:
然后,当您想要获取所有元素时当前为绿色的元素,可以通过获取附加了石灰色类的元素来实现:
如果要删除石灰色,可以使用以下命令:
You're using the jQuery .css() method incorrectly. You use .css() to get or to set a css property. For more details see: http://api.jquery.com/css/.
Instead of using css, you should add a class to all elements that you want to be lime-colored:
Then, in your css file, specify the styling for the lime-colored class:
Then, when you want to grab all of the elements that are currently green, do so by grabbing the elements that have the lime-colored class appended:
If you want to remove lime coloring, you can use the following:
您必须遍历每个然后找到属性 Lime。这是一个例子:
jQuery:你可以通过 CSS 规则而不是类进行选择吗?
另一种方法是在将背景更改为石灰时添加一个类(例如背景石灰)。然后只需搜索该类 $.(".background-lime")。
You have to loop through each then find the attribute lime. Here is an example:
jQuery: Can you select by CSS rule, not class?
Another approach though would be to add a class (like background-lime) when you change the background to lime. Then just search for that class, $.(".background-lime").