简体   繁体   English

preg替换整个div或div内容javascript?

[英]preg replace entire div or div content javascript?

I have html code(that i get from an ajax call) in a javascript string that i want to manipulate. 我在要操纵的javascript字符串中有html代码(从ajax调用中获取)。 If the div coming already exists in the string, i want to remove it or want to change its content. 如果字符串中已经存在div,我想将其删除或更改其内容。

For example i have this code in a string: 例如我在字符串中有此代码:

<div id ="message_abc_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abc</div>
<div id="messagesmsg">123</div></div></div>

<div id ="message_abcd_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abcd</div>
<div id="messagesmsg">123</div></div></div>

now suppose i get from an ajax call: 现在假设我从一个ajax调用中得到:

<div id ="message_abcd_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abcdef</div>
<div id="messagesmsg">123456</div></div></div>

how do i replace the other div with the same id? 我如何用相同的ID替换另一个div? Please note i have this in a string so i cant just remove the div 请注意,我有一个字符串,所以我不能只删除div

I feel like my teacher who told us "Well, I've been teaching them Algebra for 30 years, I figured it's about time they started getting it". 我感觉就像我的老师告诉我们:“嗯,我已经教他们代数30年了,我认为现在是他们开始学习代数的时候了”。

To get to the point: Regular expressions are insufficient in strength to parse arbitrary HTML. 要指出的是:正则表达式的强度不足以解析任意HTML。 There are a lot of edge cases and rules that cause bugs. 有很多导致错误的极端情况和规则。

There are however ways to otherwise easily solve your problem: 但是,有其他方法可以轻松解决您的问题:

The correct approach is to not have duplicate IDs to begin with. 正确的方法是没有重复的ID。 Seriously, there is almost never a good reason to have duplicate IDs. 认真地讲,几乎没有充分的理由拥有重复的ID。 They are by definition unique. 根据定义,它们是唯一的。 Simply do not add IDs to the divs in the first place. 只是首先不要将ID添加到div中。 If you need to query for them consider for a minute what you're doing: You're querying your visual presentation for business information. 如果您需要查询它们,请考虑一下您在做什么:您正在查询可视化演示中的业务信息。 Instead, use arrays and objects to represent your actual data and bind them to the presentation (with a closure or explicit mapping for example). 而是使用数组和对象表示您的实际数据,并将它们绑定到表示中(例如,使用闭包或显式映射)。

But... you already have this code.. And learning about proper UI structuring can take a lot of time.. And maybe you're not even the one creating this code on the backend. 但是...您已经有了这段代码。而且学习正确的UI结构可能会花费很多时间。.也许您甚至不是在后端创建此代码的人。

So, you can use the built in DOM parsing abilities every browser has: 因此,您可以使用每个浏览器都具有的内置DOM解析功能:

   var el = document.createElement("div"); // placeholder
   el.innerHTML = response; // feed your AJAX response there
   var elms = el.querySelectorAll("[id]"); // select every element with an ID attribute
   for(var i = 0; i < elms.length; i++){
       // check if element with the same ID exists in the document
       if(document.querySelector(elms[i].id)){
           // wipe the ID out, in your code you can do whatever with it
           elms[i].id = "";
       }
   }

Enclose the elements of your page in a <div id="target"></div> . 将页面的元素括在<div id="target"></div> and each time you get a positive response from the ajax call, just replace the existing content of div with id target within your success block of your ajax call like this 并且每次您从ajax调用中获得肯定的响应时,只需在ajax调用的成功块内将div的现有内容替换为id target

 success:function(data) {
      $('#div').html(data); // as simple as that 
 } 

Happy Coding :) 快乐编码:)

You can apply jquery selectors to the string and replace the existing content: 您可以将jquery选择器应用于字符串并替换现有内容:

var s = '<div id="message_abcd_support" class = "messageclass"><div class="messageBlockImage2"></div><div id="messageBlockText"><div id="messagesname">abcdef</div><div id="messagesmsg">123456</div></div></div>';

f = $(s).filter("#message_abcd_support");
$("#message_abcd_support").replaceWith(f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM