2 回答

TA貢獻1934條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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>
添加回答
舉報