开发者

match divs by id and delete content inside that

开发者 https://www.devze.com 2023-02-04 23:56 出处:网络
I have couple of divs like <div id=\"rg_dia_0\"> zdszczxvzxvzxvc </div> <div id=\"rg_dia_1\">

I have couple of divs like

<div id="rg_dia_0">
          zdszczxvzxvzxvc
</div>
<div id="rg_dia_1">
          dfgdZXcZC
</div>
<div id="rg_dia_2">
          hfhgjhgjdj
</div>

I need javascript to fetch all th开发者_JAVA技巧e divs and delete the content. I think we can use regex to match the ids as only the number that is changing.


As said by Sam in Javascript it can be

for (var i = 0; i <= 2; i++) {
 document.getElementById("rg_dia_" + i).innerHTML = "";
}


Something like this can easily be done with jQuery. I would strongly encourage you to learn it.

$('div').each(function() {
    $(this).html('');
});
$('#id').html(newContent);


try:

$("div[id^='rg_dia_']").html("");

see this for wildcard attribute selection in jQuery.


for (var i = 0; i <= 2; i++) {
 $("#rg_dia_" + i).html("");
}

to clarify; the above obviously requires the use of jQuery, as l.devries so blatantly down-voted as an act of true adulthood and surely not be considered as trolling since the explanation as of why had to be requested first. glad to have people like you on-board!

0

精彩评论

暂无评论...
验证码 换一张
取 消