I want to turn this: <div class="foo" id="bar"></div>
into this: <div class="foo" id="bar" style="color:black" "whatever else comes in mind" ></div>
not just css or classes etc, but whatever else comes in mind, can jquery get an element from id or class and add stuff within its < >?
is it possible?
I'm assuming that you want to add attributes, because any other kind of text added would make the markup invalid. So you can achieve it like that:
Multiple attributes at once
$(foo).attr({myNewAttribute1: "myNewAttributeValue1", myNewAttribute2: "myNewAttributeValue2"});
One attribute a time
$(foo).attr("myNewAttribute", "myNewAttributeValue");
I guess this could be a workaround
var $bar = $('#bar')
$bar.replaceWith('<div class="'+$bar.attr('class')+'" id="'+$bar[0].id+'" style="color:black" "whatever else comes in mind" ></div>');
(I went with the assumption that you don't want to add a new attribute)
找到<div class="foo" id="bar"></div>
的父项,然后
$('#parent').html("<div class="foo" id="bar" style="color:black" "whatever else comes in mind" ></div>");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.