繁体   English   中英

将location.href URL转换为Anchor链接

[英]Convert location.href URL to Anchor link

存在任何JavaScript或Objective-C方法来将location.href="MyURL"转换为<a href="MyURL"></a>

我有200多个location.href URL无法与UIWebViewNavigationTypeLinkClicked:-S一起使用

多亏大家可以帮助我!

    <a href="http://www.myurl.com" id="myAnchor"></a>

    <script type="text/javascript">
        document.getElementById("myAnchor").setAttribute("href", window.location.href);
        var loc = document.getElementById("myAnchor").getAttribute("href");
        var t = document.createTextNode(loc);
        document.getElementById("myAnchor").appendChild(t);
    </script>

这是一种实现方法:document.getElementById()获取对锚的ID属性的引用。 setAttribute()的第二个参数“ window.location.href”在当前窗口中获取对该URL的引用,并将锚中的href属性设置为此,但是如果您声明了一堆location.href()在您的代码中,然后将它们作为变量存储在第一行之前,然后在与声明“ loc”的位置相同的行处引用该变量。 变量“ loc”声明一个变量,该变量存储对您在上一行中刚刚声明的新创建的href属性的引用。 然后,我声明一个变量“ t”,该变量在DOM中创建一个文本节点,前一行的href作为该变量将要保存的值。 最后,我再次使用document.getElementById来获取“ myAnchor”,并将上一行的文本节点附加到该节点。 因此,现在我们实际上可以在链接中看到该URL。

        //Also, use a for loop to run this action 200 times and correct all of the   hrefs       on your page.
        <script type="text/javascript">
           for (var i=0;i<200;i++){
              //run document.getElementById() and .setAttribute, .createTextNode, etc. here
            }
        </script>

工作提琴: http : //jsfiddle.net/1so33q4z/36/

我不建议使用document.write(); 就像其他人的帖子中提到的那样,因为这会导致页面以DOM不需要的方式工作(写序列化文本)。 特别是如果您需要更正200个。 请参阅这篇文章为什么document.write被认为是“不良做法”?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM