使用JQuery,如何在IE中使用样式属性定位元素?(Using JQuery, how do I target an element with a style attribute in IE?)

编程入门 行业动态 更新时间:2024-10-22 23:35:14
使用JQuery,如何在IE中使用样式属性定位元素?(Using JQuery, how do I target an element with a style attribute in IE?)

使用JQuery,我的目标是:

<li style="margin-left: 15px;">blah<li>

使用此代码:

$(".block-category-navigation li[style='margin-left: 15px;']").addClass('sub-menu');

它在firefox中运行良好,但在IE中根本不起作用。 IE会忽略样式选择器吗? 无论如何,如果是这样

With JQuery, I am targeting the following:

<li style="margin-left: 15px;">blah<li>

with this code:

$(".block-category-navigation li[style='margin-left: 15px;']").addClass('sub-menu');

It works great in firefox, but doesn't work at all in IE. Does IE ignore the style selector? Anyway around if so

最满意答案

在IE8的控制台中查看后,我发现它正在将'margin-left'变成'MARGIN-LEFT'。

在IE中,这个选择器可以工作:

$(".block-category-navigation li[style='MARGIN-LEFT: 15px']").addClass('sub-menu');

您可以同时使用大写和小写选择器,也可以使用循环来检查样式属性,如下所示:

$('.block-category-navigation li[style]').each(function() { $this = $(this); if ($this.attr('style').match(/margin-left: 15px/i)) { $this.addClass('sub-menu'); } });

UPDATE

既然我不想给你不起作用的代码,我在这里设置了这个工作示例: http : //jsfiddle.net/YB7uV/6/

After checking this in IE8's console, I found that it's turning 'margin-left' into 'MARGIN-LEFT'.

In IE this selector will work:

$(".block-category-navigation li[style='MARGIN-LEFT: 15px']").addClass('sub-menu');

You could either have both upper & lower case selectors, or use a loop to check the style attribute like so:

$('.block-category-navigation li[style]').each(function() { $this = $(this); if ($this.attr('style').match(/margin-left: 15px/i)) { $this.addClass('sub-menu'); } });

UPDATE

Since I don't want to give you code that doesn't work, I've setup this working example here: http://jsfiddle.net/YB7uV/6/

更多推荐

style,IE,px,firefox,JQuery,电脑培训,计算机培训,IT培训"/> <meta name="de

本文发布于:2023-08-07 19:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465553.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   属性   元素   如何在   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!