简体   繁体   English

用Javascript删除方案后如何设置超链接样式?

[英]How to style hyperlink after scheme is removed with Javascript?

So I am trying to display a url that is caught by Javascript. 因此,我试图显示一个Javascript捕获的URL。 It is returned to JS in the form http://i.mygreatsite.com , but I would like to cut the schema off to make it cleaner, and have the actual code look something like 它以http://i.mygreatsite.com的形式返回给JS,但是我想将模式切掉以使其更整洁,并使实际代码看起来像

<a href=http://i.mygreatsite.com>i.mygreatsite.com</a>

Before I tried doing this, I had to set some special attributes on the url, namely setting target to blank so it would open in a new tab and changing the color of the hyperlink to black, as the rest of the links on the page are blue. 在尝试执行此操作之前,我必须在url上设置一些特殊属性,即将target设置为blank,以便它将在新选项卡中打开并将超链接的颜色更改为black,因为页面上的其余链接都是蓝色。 I went about it like this: 我这样处理:

  init: function() {
                this.on("success", function(file, responseText) {
                    var span = document.createElement('span');
                    var a = document.createElement('a');
                    a.href = responseText;
                    a.innerHTML = responseText;
                    a.setAttribute('target', '_blank');
                    a.style.color="black";
                    span.appendChild(a);
                    span.setAttribute("style", "position: absolute; box-sizing: border-box; -webkit-box-sizing: border-box; bottom: -28px; left: 3px; right: 3px;  height: 28px; word-wrap: break-word; line-height: 28px; text-overflow: ellipsis; ");

                    file.previewTemplate.appendChild(span);

                });
            }

Everything worked fine with it, but when I went to remove the schema I did it by using regex: 一切工作都很好,但是当我删除架构时,我使用了regex:

repl = responseText.replace(/(https?:\/\/(\S+))/i, "<a href='$1'>$2</a>");
a.href = repl;
a.innerHTML = repl;
a.setAttribute('target', '_blank');
a.style.color="black";

Now the schema is removed, but none of the attributes I set previously that were working before are there now. 现在,该架构已删除,但是我之前设置的所有以前可用的属性现在都没有。 The URL is blue and opens in the same tab. 该URL为蓝色,并在同一选项卡中打开。 Am I missing something here? 我在这里想念什么吗? Thanks. 谢谢。

What you've ended up with is an anchor within another anchor: 您最终得到的是另一个锚点内的锚点:

<a target="_blank" style="XXX"><a href="http://myurl">myurl</a></a>

That's because of setting the innerHTML of the anchor you're trying to create. 那是因为设置了您要创建的锚的innerHTML Setting styles on an element will not cascade (you should really use CSS Stylesheets instead) and of course the target element will not apply as it's technically not the hyperlink you're clicking on! 在元素上设置样式不会层叠(您应该真正使用CSS样式表),并且当然, target元素将不适用,因为从技术上讲,它不是您单击的超链接!

Instead, just set the innerHTML using the $2 result of your Regex. 而是使用Regex的$2结果设置innerHTML

Edit 编辑

So your updated code would need to be similar to: 因此,您更新的代码将需要类似于:

repl = responseText.replace(/(https?:\/\/(\S+))/i, "$2");
a.href = responseText;
a.innerHTML = repl;
a.setAttribute('target', '_blank');
a.style.color="black";
//Or if you wanted to use CSS stylesheets
a.className = "myCssClass"

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

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