我想fill
用jQuery.css()
方法并通过data- *属性单击它时,更新Circle元素的css属性(例如,从蓝色更改为红色)。
例子:
的CSS
svg {
height: 200;
width: 200;
}
circle {
fill: blue;
}
JAVASCRIPT
jQuery(function(){
$(document).on("click", "[data-update]", function(){
var circle = $(this).find("circle");
circle.css({ fill: "red" });
});
});
的HTML
<svg data-update>
<circle cy="100" cx="100" r="50"></circle>
</svg>
演示的链接在这里。
因此,如果我向data-update
属性添加一个值(例如绿色):
data-update="green"
那么,为了更改Circle元素的颜色,我应该在脚本中更改或添加哪些特定代码?
任何想法?
您只需要获取属性的值。演示。
jQuery(function(){
$(document).on("click", "[data-update]", function(){
var circle = $(this).find("circle");
circle.css({
fill: $(this).data('update') // <-- get value
});
});
});
<svg data-update="green">
<circle cy="100" cx="100" r="50"></circle>
</svg>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句