简体   繁体   English

jQuery HTML在第二次加载后替换动态加载的Ajax内容

[英]jQuery HTML Replace Dynamically Loaded Ajax Content After Second Load

After about 20 hours of compulsive searching I have managed to get text replace to start working after dynamic content is executed to apply on the dynamic content. 经过大约20小时的强制搜索,在设法将动态内容应用于动态内容之后,我设法替换了文本以开始工作。

$("body").one("mouseover", "span.help-more", function(index) {
    $('li#li').each(function(index) {
        var $this = $(this);
        var t = $this.text();
        $this.html(t.replace("&lt", "<").replace("&gt", ">"));
    });
});

However, once dynamic content is once again retrieved the second time or any time after that, the text is no longer replaced. 但是,一旦第二次或之后的任何时间再次检索动态内容,则不再替换文本。

I tried to get around this (see example below) by removing "one" and changing it with "on", then copying the code to undo the events (reversing the t.replace) but that didn't work. 我试图通过删除“一个”并将其更改为“开”来解决此问题(请参见下面的示例),然后复制代码以撤消事件(反转t.replace),但这没有用。

$("body").on("mouseover", "span.help-more", function(index) {
    $('li#li').each(function(index) {
        var $this = $(this);
        var t = $this.text();
        $this.html(t.replace("&lt", "<").replace("&gt", ">"));
    });
});

$("body").on("mouseout", "span.help-more", function(index) {
    $('li#li').each(function(index) {
        var $this = $(this);
        var t = $this.text();
        $this.html(t.replace("<", "&lt").replace(">", "&gt"));
    });
});

Can someone please help me? 有人可以帮帮我吗? I've given this about 20 hours so far. 到目前为止,我已经提供了大约20个小时。 I would be happy with any solutions. 我将对任何解决方案感到满意。

I should mention : I would just run it "on", but then somehow the HTML tags get stripped away upon second execution, that's why I tried to execute, undo. 我应该提到 :我只是在“ on”上运行它,但是以某种方式在第二次执行时剥离了HTML标记,这就是为什么我尝试执行,撤消。

I would be happy if either: -(1) "one" is refired after every click -(2) "on" if i can undo changes onmouseout -(3) "on" and it stops stripping away tags on second run ..or any option you may suggest that is effective.. 如果以下任一情况,我将感到高兴:-(1)每次单击后重新触发-(2)“打开”,如果我可以撤消onmouseout的更改-(3)“打开”,并且它在第二次运行时停止剥离标签。或您可能认为有效的任何选择。

Your help would be immensely appreciated! 您的帮助将不胜感激!

edit:in case this helps The Relevant HTML: 编辑:如果这有助于相关HTML:

    <ul>
    <li class="stone-list ng-scope" id="stone_913"><span class="check-span"><span class="check-span-botton"></span></span> <span class="shape-span ng-binding">pear</span> <span class="carat-span ng-binding">0.5</span> <span class="cut-span ng-binding">excellent</span> <span class="colour-span ng-binding">G</span> <span class="clarity-span ng-binding">VS2</span> <span class="price-span" content="stone.price" dir=""><span class="ng-scope">$ 1412</span></span> <span class="help-span"><span class="help-more"><span class="information-help-more"><span class="top-help-more"></span> <span class="center-help-more"></span></span></span></span></li>
</ul>
<ul>
    <!-- ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Shape pear</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Carat Weight 0.5</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Clarity VS2</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Color G</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Color Shade WH</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Cut excellent</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Polish VG</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Symmetry VG</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Fluoresence N</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Depth (%) 62.5</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Table (%) 61</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Certificate Type</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Number 6255152356</span></li><!-- end ngRepeat: row in stone.tooltrip -->
    <li class="ng-binding ng-scope" id="li"><span class="center-help-more"><a href="[link]" style="color:yellow">Video Link</a></span></li>
    <li style="list-style: none"><span class="center-help-more"><!-- end ngRepeat: row in stone.tooltrip --></span></li>
</ul><span class="bottom-help-more"></span> <span class="change-span"><span class="change-span-botton">Choose</span></span>
<ul></ul>

It's some crappy html tag replace by WordPress that's causing me to find a workaround. 这是用WordPress替换的一些糟糕的html标签,这使我找到了解决方法。

Number of fixes to make. 进行的修复数量。 Here is a Working Example: https://jsfiddle.net/Twisty/3cp2kddm/ 这是一个工作示例: https : //jsfiddle.net/Twisty/3cp2kddm/

HTML HTML

<ul>
  <li class="stone-list ng-scope" id="stone_913">
    <span class="check-span">
    <span class="check-span-botton"></span>
    </span>
    <span class="shape-span ng-binding">pear</span>
    <span class="carat-span ng-binding">0.5</span>
    <span class="cut-span ng-binding">excellent</span>
    <span class="colour-span ng-binding">G</span>
    <span class="clarity-span ng-binding">VS2</span>
    <span class="price-span" content="stone.price" dir="">
    <span class="ng-scope">$ 1412</span>
    </span>
    <span class="help-span">
    <span class="help-more">
      <span class="information-help-more ui-icon ui-icon-info">
        <span class="top-help-more"></span>
    <span class="center-help-more"></span>
    </span>
    </span>
    </span>
  </li>
</ul>
<ul>
  <!-- ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Shape pear</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Carat Weight 0.5</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Clarity VS2</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Color G</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Color Shade WH</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Cut excellent</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Polish VG</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Symmetry VG</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Fluoresence N</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Depth (%) 62.5</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Table (%) 61</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Certificate Type</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more">Number 6255152356</span></li>
  <!-- end ngRepeat: row in stone.tooltrip -->
  <li class="ng-binding ng-scope" id="li"><span class="center-help-more"><a href="[link]" style="color:yellow">Video Link</a></span></li>
  <li style="list-style: none"><span class="center-help-more"><!-- end ngRepeat: row in stone.tooltrip --></span></li>
</ul><span class="bottom-help-more"></span> <span class="change-span"><span class="change-span-botton">Choose</span></span>
<ul></ul>

JavaScript JavaScript的

$(function() {
  $(".stone-list").on("mousein mouseenter mouseleave mouseout", ".help-more", function(e) {
    $('.ng-binding').each(function(ind, el) {
      var $this = $(el);
      var t = $this.prop("innerHTML");
      console.log("HTML Content: ", t);
      if (e.type == "mouseleave") {
        t = t.replace(/&lt;/g, "<");
        t = t.replace(/&gt;/g, ">");
      } else {
        t = t.replace(/</g, "&lt;");
        t = t.replace(/>/g, "&gt;");
      }
      console.log("HTML Converted: ", t.toString());
      $this.html(t);
    });
  });
});

First, you have non-unique id attribute #li on almost all of your li elements. 首先,几乎所有li元素都具有非唯一id属性#li This defeats your .each() function. .each()您的.each()函数失败。 The id attribute should be unique. id属性应该是唯一的。 I would suggest <li id="li-1"></li> where each id has a unique number. 我建议<li id="li-1"></li> ,其中每个ID都有一个唯一的数字。

Second, I assume your script has some object or image inside of <span class="help-more"></span> such that the user can mouse over it. 其次,我假设您的脚本在<span class="help-more"></span>内有一些对象或图像,以便用户可以将鼠标悬停在其上。 I made use of jQuery UI to do this in my example. 在示例中,我使用了jQuery UI。 Since the event could be worded differently in different browsers, I suggest binding to a number of them. 由于在不同的浏览器中事件的措词可能不同,因此我建议绑定到许多浏览器。 Then performing an action conditionally based on the event you are looking for. 然后根据您要查找的事件有条件地执行操作。 This can be done with: 这可以通过以下方式完成:

$( selector ).on( "mouseenter mouseleave", handlerInOut );

As you want to replace < or > with their HTML Entities, those being &lt; 当您想用其HTML实体替换<> ,它们是&lt; and &gt; &gt; , make sure you're using the right entities. ,请确保您使用的是正确的实体。 Your replacement function did not work globally; 您的替换功能无法全局运行; hence, it only replaced the first occurrence. 因此,它仅替换了第一次出现的情况。 Switching to a regular expression with the global flag addresses this. 使用全局标志切换到正则表达式可以解决此问题。

t = t.replace(/</g, "&lt;");
t = t.replace(/>/g, "&gt;");

Calling .text() will only get you the actual text value and not the HTML value. 调用.text()只会获得实际的文本值,而不是HTML值。 Advise using .html() or .prop("innerHTML") . 建议使用.html().prop("innerHTML") Both supply the same results. 两者提供相同的结果。

Hope that helps. 希望能有所帮助。

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

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