使用jquery用文本替换图像

时间:2013-09-29 00:31:44

标签: javascript jquery

我的HTML代码看起来像这样

<a class="wsl_connect_with_provider" title="Connect with Facebook" data-provider="Facebook">
<img src="/assets/img/facebook.png">
</a>
<a class="wsl_connect_with_provider" title="Connect with Twitter" data-provider="Twitter">
<img src="/assets/img/twitter.png">
</a>

我想像img一样使用title替换jquery

<a class="wsl_connect_with_provider" title="Connect with Facebook" data-provider="Facebook">
    Connect with Facebook
    </a>
    <a class="wsl_connect_with_provider" title="Connect with Twitter" data-provider="Twitter">
    Connect with Twitter
    </a>

有人可以告诉我该怎么做吗?

3 个答案:

答案 0 :(得分:2)

$.each($('img'), function(){
  $(this).replaceWith('<span>'+$(this).attr('title')+'</span>');
});

答案 1 :(得分:1)

这应该有效:

$('a.wsl_connect_with_provider').each(function(){
  $(this).text($(this).prop('title'));
});

演示here

答案 2 :(得分:1)

$('.wsl_connect_with_provider img').each(function () {
    $(this).replaceWith(
        $(this.parentNode).attr('title')
    );
});