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


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


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



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


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


 jQuery(“。tmpl -popup“)。each(function(index){

 var haystack = jQuery(this).html(),
 needle = haystack.replace(/ %% THEMEPATH %% / g ,themePath);

 jQuery(this).html(needle);

 var popup = jQuery(jQuery.parseHTML(haystack)),
 imageSrc = jQuery (popup).find('img')。data('image'),

 jQuery(popup).find('img')。attr('src',imageSrc);&#xA ;
 jQuery(this).html(popup);

});



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

答案 0 :(得分:1)
您可以使用jQuery' 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;
});
});
$("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;
});
});

<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;