用Jquery替换<script>标记内的HTML中的值

时间:2017-04-23 11:37:04

标签: javascript jquery html parsing replace

我周末的大部分时间都在盲目看似简单的修复,因此我转向你们的专业知识。

&#xA;&#xA;

我有X以下脚本标记的实例(但始终具有相同的类名):

&#xA;&#xA;
 &lt; script type =“text-html”class =“tmpl-popup “&gt;&lt; div class =”acf-fc-popup“&gt;&#xA; &LT; UL&GT;&#XA; &LT;李&GT;&#XA; &LT; A HREF = “#” 的数据布局= “one_column” 数据分钟= “” 数据-MAX = “” &GT;&LT; IMG SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII =” 数据图像= “%% THEMEPATH %% / images / admin / layouts / one_column.png”/&gt; &lt; span&gt;一列&lt; / span&gt;&lt; / a&gt;&#xA; &LT; /立GT;&#XA; &LT;李&GT;&#XA; &LT; A HREF = “#” 的数据布局= “two_columns” 数据分钟= “” 数据-MAX = “” &GT;&LT; IMG SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII =” 数据图像= “%% THEMEPATH %% / images / admin / layouts / two_columns.png”/&gt; &lt; span&gt;两列&lt; / span&gt;&lt; / a&gt;&#xA; &LT; /立GT;&#XA; &LT; / UL&GT;&#XA; &lt; a href =“#”class =“focus”&gt;&lt; / a&gt;&#xA; &lt; / div&gt;&#xA;&lt; / script&gt;&#xA;  
&#xA;&#xA;

我首先需要替换%% THEMEPATH %%使用包含站点名称的变量,该站点名称可以是“ http://mydomainname.com ”。当替换完成后,我需要将脚本标签内的img标签的src替换为值data-image值。

&#xA;&#xA;

但我似乎无法获得它以我想要的方式工作,因为HTML在脚本标记内。这是我到目前为止所做的工作,但它似乎并没有真正完成这项工作:(

&#xA;&#xA;
  jQuery(“。tmpl -popup“)。each(function(index){&#xA;&#xA; var haystack = jQuery(this).html(),&#xA; needle = haystack.replace(/ %% THEMEPATH %% / g ,themePath);&#xA;&#xA; jQuery(this).html(needle);&#xA;&#xA; var popup = jQuery(jQuery.parseHTML(haystack)),&#xA; imageSrc = jQuery (popup).find('img')。data('image'),&#xA;&#xA; jQuery(popup).find('img')。attr('src',imageSrc);&#xA ;&#xA; jQuery(this).html(popup);&#xA;&#xA;});&#xA;  
&#xA;&#xA;

所以如果有人能帮助我,请提前感谢:(

&#xA;

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery&#39; s html的函数回调版本,并使用%%THEMEPATH%%部分的全局字符串替换,然后(如您所见)解析HTML和句柄返回源代码之前的图像更新script元素文本:

$("script.tmpl-popup").html(function(_, html) {
  // Update %%THEMEPATH%%
  html = html.replace(/%%THEMEPATH%%/g, "http://example.com");

  // Update src: `$.parseHTML` gives us an array of the top-level elements.
  // we loop through them, finding any `img[data-image]` that they contain,
  // and updating the `src` on those.
  return $.parseHTML(html).map(function(element) {
    // Note: If it were possible for an `img[data-image]` to be *at* the top
    // level, you'd need: `$(element).find("img[data-image]").addBack("img[data-image").attr...
    $(element).find("img[data-image]").attr("src", function() {
      return this.getAttribute("data-image"); // or $(this).attr("data-image") if you want more jQuery :-)
    });
    return element.outerHTML;
  });
});

&#13;
&#13;
$("script.tmpl-popup").html(function(_, html) {
  // Update %%THEMEPATH%%
  html = html.replace(/%%THEMEPATH%%/g, "http://example.com");
  
  // Update src: `$.parseHTML` gives us an array of the top-level elements.
  // we loop through them, finding any `img[data-image]` that they contain,
  // and updating the `src` on those.
  return $.parseHTML(html).map(function(element) {
    // Note: If it were possible for an `img[data-image]` to be *at* the top
    // level, you'd need: `$(element).find("img[data-image]").addBack("img[data-image").attr...
    $(element).find("img[data-image]").attr("src", function() {
      return this.getAttribute("data-image"); // or $(this).attr("data-image") if you want more jQuery :-)
    });
    return element.outerHTML;
  });
});
&#13;
<p>You'll have to use right-click Inspect Element to see that the change has happened. :-)</p>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script type="text-html" class="tmpl-popup"><div class="acf-fc-popup">
        <ul>
                                <li>
                    <a href="#" data-layout="one_column" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/one_column.png" /> <span>One Column</span></a>
                </li>
                                <li>
                    <a href="#" data-layout="two_columns" data-min="" data-max=""><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-image="%%THEMEPATH%%/images/admin/layouts/two_columns.png" /> <span>Two Columns</span></a>
                </li>
                        </ul>
        <a href="#" class="focus"></a>
    </div>
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;