【问题标题】:Issue with changing the title of a link using jQuery使用 jQuery 更改链接标题的问题
【发布时间】:2017-12-26 17:28:46
【问题描述】:

我一直在尝试使用 JQuery 更改链接的标题。基本上,即使我键入 www.hotmail.com,我当前的主题也存在将某些内容识别为 URL 的问题。我想:

(1) 让它识别出它是指向 www.hotmail.com 的链接

(2) 将 www.hotmail.com 改名为 Click Here

我已尝试实现此线程中提到的内容,但未成功 (How do I change the title of a link using jQuery)。

单击后,我能够获得指向 www.hotmail.com 的链接,但文本并未更改为单击此处。

我的代码:

var href = jQuery('p.item-property').html();
var link = "<a href='"+href+"' target='_blank'>"+href+"</a>";

jQuery('p.item-property').replaceWith(link);

$('p.item-property').text('Click Here');

【问题讨论】:

    标签: jquery


    【解决方案1】:

    当您替换 p.item-property 时,它不再存在,因此文本不会更改。

    我建议你使用jQuery(html, attributes) 创建元素,它可以很容易地操作。

    var href = jQuery('p.item-property').html();
    
    //Create element using jQuery
    var link = $('<a>', {
      href: href,
      text: href //<== You can directly set here 'Click Here'
    });
    
    //Replace the element
    jQuery('p.item-property').replaceWith(link);
    
    //Update the text
    link.text('Click Here');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p class="item-property">http://link.com</p>

    【讨论】:

    • 感谢您的回复。不知道为什么我不能让它工作。感谢您的帮助。
    【解决方案2】:

    您的选择器标识与您已替换为link 的元素相对应的元素。您想改为更改 linktext

    var href = jQuery('p.item-property').html();
    var link = "<a href='"+href+"' target='_blank'>"+href+"</a>";
    
    jQuery('p.item-property').replaceWith(link);
    
    $(link).text('Click Here');
    

    但最好在初始化时设置正确的text

    var href = jQuery('p.item-property').html();
    var link = "<a href='"+href+"' target='_blank'>Click Here</a>";
    
    jQuery('p.item-property').replaceWith(link);
    

    【讨论】:

    • 我一直在尝试将 p.item-property 更改为不同的项目 '#c27-single-listing &gt; section &gt; div.profile-cover-content.reveal.reveal_visible &gt; div &gt; div &gt; ul &gt; li:nth-child(3) &gt; div' 但收到错误消息 Error 404: The page your are looking for cannot be found. 您是否知道原因?
    • 如果不清楚,请在此处发布我的查询 --> link。谢谢
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多