如何使用特定的样式属性进行选择?jQuery:选择样式属性?
<div style="width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px;">
我想:
$("div[style='width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px;']);
,但没有被选中。
如何使用特定的样式属性进行选择?jQuery:选择样式属性?
<div style="width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px;">
我想:
$("div[style='width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px;']);
,但没有被选中。
你的榜样为我工作,在Chrome中至少有一次我纠正了在该行的末尾所缺失的“错字。
$("div[style='width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px;']");
(这不回答这个问题。但是,如果谁写的HTML页面的人曾使用过,而不是风格属性类,那么OP就没有这个问题。)
.foo { width: 420px; text-align: left; margin-top: 10px; margin-bottom: 10px; }
<div class="foo"> </div>
$("div.foo")
也许尝试
$('div').filter(function() {
var self = $(this);
return self.width() === 420 &&
self.css('margin-top') === '10px' &&
self.css('margin-bottom') === '10px' &&
self.css('text-align') === 'left';
});
它会更容易然而,由一个属性,如ID或CSS类选择元素,或者它的位置,有一个ID或者CSS类的元素。
据我所知,有没有办法做到这一点使用jQuery选择,但你可以使用filter()方法代替:
$("div").filter(function() {
var $this = $(this);
return $this.css("width") == "420px"
&& $this.css("text-align") == "left"
&& $this.css("margin-top") == "10px"
&& $this.css("margin-bottom") == "10px";
});
你为什么不使用类/ IDS选择的东西?而你为什么要使用内联`风格=单曲tyles? – thejh 2010-11-27 12:21:59
@thejh有时使用内联样式有很好的理由。如果每次有许多对象与基础样式以独特的方式不同,那么每次设置一个新的ID通常是不必要的,或者对于动态代码而言不适用。 – Orbling 2010-11-27 12:23:55
我试图从现有的html页面中提取数据。 – davidgale 2010-11-27 12:24:22