2 回答

TA贡献1934条经验 获得超2个赞
您可以使用属性选择器,例如
$("rect[x=0][y=0]")
这是一个例子:
$("div[data-x]").css("color", "red")
$("div[data-x=0]").css("font-style", "italic")
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div data-x="0" data-y="0">div 0x0</div>
<div data-x="100" data-y="100">div 100x100</div>

TA贡献1859条经验 获得超6个赞
工作示例:在这个示例中,我将每个attribute乘以4
function changeFunc(){
var elementsLength = $('div').length;
console.log(elementsLength);
for(var index = 1; index <= elementsLength; index++){
var nthRect = $('div:nth-of-type('+index+')');
nthRect.attr('width',nthRect.attr('width') * 4);
nthRect.attr('height',nthRect.attr('height') * 4);
nthRect.attr('x',nthRect.attr('x') * 4);
nthRect.attr('y',nthRect.attr('y') * 4);
nthRect.css("width",nthRect.attr("width"));
nthRect.css("height",nthRect.attr("height"));
}
}
$( document ).ready(function() {
var elementsLength = $('div').length;
for(var index = 1; index <= elementsLength; index++){
var nthRect = $('div:nth-of-type('+index+')');
nthRect.css("width",nthRect.attr("width"));
nthRect.css("height",nthRect.attr("height"));
}
setTimeout(changeFunc,1000)
});
div{
background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div x="0" y="0" width="36" height="3"></div>
<div x="17" y="8" width="19" height="3"></div>
<div x="0" y="16" width="36" height="3"></div>
添加回答
举报