Replace http:// in anchor portion of links using Jquery

北战南征 提交于 2019-12-11 23:31:57

问题


on a page there a several links of:

<a class="linked" href="http://link1.com>http://link1.com</a>

<a class="linked" href="http://link2.com>http://link2.com</a>

How would one remove the second http:// in each link so it can't be seen on the screen.

I've tried this to no avail:

$(document).ready(function() {

$('.linked').html().replace("http://","");

回答1:


If you are talking about the visible text in an anchor tag

$.each($('.linked'), function()
{
  var anchor = $(this);
  anchor.text( anchor.text().replace("http:\/\/",'') )
});

Missing ');' at the end...




回答2:


Just for the record, the jQuery-less version:

var links = document.links;
for(var i = links.length; i--; ) {
    with(links[i]) {
        if(/(^|\s)linked(\s|$)/.test(className)) {
            firstChild.nodeValue =
                firstChild.nodeValue.replace(/^http:\/\//, '');
        }
    }
}


来源:https://stackoverflow.com/questions/638488/replace-http-in-anchor-portion-of-links-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!