Javascript:所需的解决方法:Internet Explorer在更改href时更改链接文本 [英] Javascript: Workaround needed: Internet Explorer changes link text when changing the href

查看:78
本文介绍了Javascript:所需的解决方法:Internet Explorer在更改href时更改链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如下所示:我们在我们的应用程序中使用了富文本编辑器(TinyMCE,但这在这里并不重要).现在,在Internet Explorer 8中,我们注意到,如果您键入看起来像网址的内容:

I have a problem as follows: We're using a rich text editor (TinyMCE, but that's not important here, I think) in our application. Now, with Internet Explorer 8, we've noticed that if you type in content that looks like a web address:

www.google.com

www.google.com

... IE通过某些从本机到浏览器的功能将其有用地转换为链接.现在,如果您真的要使其成为链接,并选择编辑链接属性,并设置href例如到

...IE helpfully converts it to a link by some native-to-browser functionality. Now if you really want to make it into a link, and choose to edit link properties, and set the href e.g. to

www.google.com/analytics

www.google.com/analytics

...然后,当javascript设置锚标记的href属性时,链接的文本也会更改.理想的结果是:

...then when the javascript sets the href attribute of the anchor tag, also the text of the link changes. The desired result is:

`<a href="http://www.google.com/analytics">www.google.com</a>`

但实际上是:

`<a href="http://www.google.com/analytics">www.google.com/analytics</a>`

有人知道解决此问题的方法吗?

Does anyone know a way to work around this?

更新:仅在Internet Explorer 8和7中观察到此行为.Firefox,Chrome和Safari不受影响.还可以在TinyMCE网站上观察到该问题, http://tinymce.moxiecode.com/examples/full.php ,因此可能不是TinyMCE配置问题.

Update: This behavior has only been observed in Internet Explorer 8 and 7. Firefox, Chrome, and Safari are not affected. The problem can also be observed on the TinyMCE websitehttp://tinymce.moxiecode.com/examples/full.php, so it's probably not a TinyMCE configuration issue.

推荐答案

经过研究和调试,我发现问题是由Internet Explorer的内置行为引起的.设置链接的href属性时会发生这种情况,该链接的文本内容似乎是URL(根据IE).在这种情况下,IE会将href属性的内容复制到链接文本中.

After some research and debugging, I found that the problem is caused by built-in behavior of Internet Explorer. It occurs when setting the href-property of a link, whose text-content appears to be an URL (according to IE). In these cases, IE copies the contents of the href-attribute into the link text.

可能有几种解决方法,但我发现至少此逻辑有效:

There might be several workarounds for this, but I found that at least this logic works:

  1. innerHTML存储到一个临时变量
  2. 照常设置href属性
  3. 如果innerHTML已更改,请从临时变量复制回原始的innerHTML.
  1. store the innerHTML into a temporary variable,
  2. set the href attribute as usual
  3. if innerHTML has changed, copy back the original innerHTML from the temporary variable.

这似乎有用,因为更改链接的innerHTML不会导致更改href属性.

This seems to work because changing the innerHTML of the link does not cause changing of the href attribute.

在tinyMCE中,在advlink插件的functions.js的setAllAttribs()中找到以下行:

In tinyMCE, find the following line in setAllAttribs() of functions.js of the advlink plugin:

setAttrib(elm, 'href', href);

...并用这个怪物替换它:

...and replace it with this monster:

if(tinyMCE.isMSIE) {
    var tmp = elm.innerHTML;
    setAttrib(elm, 'href', href);
    if(elm.innerHTML != tmp) // optional, but keeps unnecessary innerHTML set:s away
        elm.innerHTML = tmp;
}
else {
    setAttrib(elm, 'href', href);
}

...,并且您的链接将显示为未修改状态.我也在tinyMCE论坛上启动了一个话题.如果他们发布了对我的解决方案的一些改进或说废话,我也将更新此问题.

...and your links will appear as if untouched. I also started a thread on the tinyMCE forums about this. If they post some improvements to my solution or tell it's nonsense, I'll update this question also.

这篇关于Javascript:所需的解决方法:Internet Explorer在更改href时更改链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆