JS将href改为HTML编码实体中的onclicks

时间:2016-01-18 07:17:30

标签: javascript jquery html html-entities

我正在循环遍历包含HTML实体的字符串的数据库:

"<!-- SC_OFF --><div class=\"md\"><p>Here is an <a href=\"http://example.com\">example link</a></p>\n\n<p>Here is some body text for you with <sup>superscript</sup> and a bit of <strong>boldness</strong> and some <em>italics</em>. I forgot to mention this regular link here <a href=\"http://example.com\">http://example.com</a&gt; so don&#39;t forget that one. sometimes there can be more than one <a href=\"http://example.com\">formatted link</a> and <a href=\"http://example.com\">http://example.com</a></p&gt;\n\n<ul>\n<li>so there you go</li>\n<li>some sample text to work with</li>\n</ul>\n\n<p><del>strikethrough</del></p>\n\n<pre><code>if(canCode){\n //lol dont kid yourself\n}\n</code></pre>\n</div><!-- SC_ON -->"

将动态插入DOM。我需要改变链接的作用基本上所有这些:

<a href="http://example.com">example</a>

变为:

<a onClick="myFunc('http://example.com')">example</a>

您可以在此处找到解码后的字符串:

http://codepen.io/YikesItsMikes/pen/NxaJXg

但我想编辑原始文字可能更容易?老实说我不知道​​:s

5 个答案:

答案 0 :(得分:3)

将html元素添加到DOM后。以下代码工作(未经测试),

0b[0-1]+

更新了示例中的代码(已测试)

var $aTags = $('div.md').find('a');

$aTags.each(function() {
   var $aTag = $(this),
       href = $aTag.attr('href');
   $aTag.on('click',function(e) {
      myfunc(href);
      e.preventDefault();
   });
});

答案 1 :(得分:2)

NSDictionary

答案 2 :(得分:2)

在插入之前,您可以运行此replace: -

html = html.replace(/href=\"([^\"]+)\"/gi, 'onClick="myFunc(\'$1\')"')

使用群组捕获和$1

正则表达式中的( ... )组将值1捕获为0始终为完全捕获,在您的情况下为href="http://example.com"

在第二个参数中,您可以使用$0作为完整,或$1作为捕获组。

答案 3 :(得分:1)

function myFunction(link) {
  console.log(link)
}

var html = "<!-- SC_OFF --><div class=\"md\"><p>Here is an <a href=\"http://example.com\">example link</a></p>\n\n<p>Here is some body text for you with <sup>superscript</sup> and a bit of <strong>boldness</strong> and some <em>italics</em>. I forgot to mention this regular link here <a href=\"http://example.com\">http://example.com</a&gt; so don&#39;t forget that one. sometimes there can be more than one <a href=\"http://example.com\">formatted link</a> and <a href=\"http://example.com\">http://example.com</a></p&gt;\n\n<ul>\n<li>so there you go</li>\n<li>some sample text to work with</li>\n</ul>\n\n<p><del>strikethrough</del></p>\n\n<pre><code>if(canCode){\n    //lol dont kid yourself\n}\n</code></pre>\n</div><!-- SC_ON -->";

$(html).filter(":not(comment)")
.find("a").each(function() {
  var link = this.href;
  $(this)
  .attr({"href": "#", "onclick":"myFunction('"+link+"')"}) 
}).addBack().appendTo("body")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

答案 4 :(得分:1)

 $(document).ready(function () {

     var szData="<div class=\"md\"><p>Here is an <a href=\"http://example.com\">example</a></p>\n\n<p>Here is some body text for you with <sup>superscript</sup> and a bit of <strong>boldness</strong> and some <em>italics</em>. I forgot to mention this regular link here <a href=\"http://example.com\">http://example.com</a&gt; so don&#39;t forget that one. sometimes there can be more than one <a href=\"http://example.com\">formatted link</a> and <a href=\"http://example.com\">http://example.com</a></p&gt;\n\n<ul>\n<li>so there you go</li>\n<li>some sample text to work with</li>\n</ul>\n\n<p><del>strikethrough</del></p>\n\n<pre><code>if(canCode){\n    //lol dont kid yourself\n}\n</code></pre>\n</div>"


     $('.content').html(szData);

     $('.content').find('a').each(function () {


         $(this).on('click', function (e) {
             myfunc($(this).attr('href'));
             e.preventDefault();
         });

         $(this).removeAttr('href');
     });


     function myfunc(szhref)
     {
         alert('myfunc')
     }
});