![](/img/trans.png)
[英]How to find and replace begin tag and end tags in an html string using javascript/jquery
[英]how to find string in html tag and replace using javascript/jquery
我不知道如何在html標記中找到一個字符串並將其替換為另一個。 我將在一個示例中展示它。
我有這樣的東西
<a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
我想得到這樣的效果
<a class="test"> <img-src="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
有人有什么主意嗎? 我對我的英語水平感到抱歉。
您可以先append
img
,然后刪除屬性data-value
。
請參見下面的代碼:
$("a").append(function(){ return '<img src="'+$(this).attr("data-value")+'">'; }).removeAttr("data-value")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
假設您是JavaScript
和/或jQuery
新手,我逐步給出了解決方案的示例:
// create a pointer to your anchor
var $yourAnchor = $('.test')
// copy data attribute
var $hdata = $yourAnchor.data('value');
// remove attribute
$yourAnchor.removeAttr('data-value');
// insert html
$yourAnchor.html('<img src="' + $hdata + '">');
// If It has multiple hyperlink tag then $('a').each(function(){ if($(this).data('value')!=undefined){ $(this).append(function(){ return '<img src="'+$(this).attr("data-value")+'">'; }).removeAttr("data-value"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.