0

次のリンクをクリーンアップする方法を探していました:

    <a href="javascript:void((function()%7Bvar%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)%7D)());"><img src="/images/Social%20Media/pinterest.png"/></a>

のようなものに

<a id="pinterest><img src="/images/Social%20Media/pinterest.png"/></a>

そしてJqueryを介したhref部分

4

3 に答える 3

1

私はあなたがこれを望んでいると思います

HTML

<a id="pinterest" href="">
    <img src="/images/Social%20Media/pinterest.png"/>
</a>​

JS

$('#pinterest').attr("href","javascript:void((function()%7Bvar%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)%7D)());");​

ここに例があります。例の画像をクリックして確認してください。

于 2012-04-11T21:40:17.573 に答える
0

JSでリンクを変更するだけの場合:

$('a[href^="javascript:void((function()%7Bvar%20e="]').replaceWith('<a id="pinterest"><img src="/images/Social%20Media/pinterest.png"/></a>')​;

すでに外部スクリプトがあり、マークアップを変更できる場合は、HTMLを次のように変更するだけです。

<a id="pinterest"><img src="/images/Social%20Media/pinterest.png"/></a>

と:

$('#pinterest').on('click', function(e) {
    e.preventDefault();
    $.getScript('http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);
});

ここにフィドルがあります->それが機能していることを示しますか?

于 2012-04-11T21:25:35.970 に答える
0

まず、リンクから属性 href を削除する必要があると思います。現在、それを取得する唯一の方法は、最良のアイデアではない可能性がある要素名によるものです。

$("a").removeAttr("href")

次に、属性IDをリンクに追加できます

$("a").attr( "id" , "pinterest");
于 2012-04-11T21:15:31.353 に答える