简体   繁体   English

在将内容从div复制到页面标题时,如何防止对'&'字符进行HTML编码?

[英]How do I prevent HTML encoding of '&' characters while copying content from a div to the title of the page?

I have a div whose contents are My Home & Administration 我有一个div,其内容是我的家和管理
<div id="abc"> My Home & Administration></div>

In the scripts, I need to copy the contents of this div to my page title. 在脚本中,我需要将此div的内容复制到我的页面标题。 Currently I am using innerHTML property which encodes the value. 目前我正在使用innerHTML属性来编码值。

<script type="text/javascript>
var id = document.getElementById("abc");
document.title = id.innerHTML;
</script>

The title is displayed as My Home & amp; 标题显示为My Home&amp; Administration (Note: disregard the space between & and amp;) 管理 (注意:忽略&和amp;之间的空间)

Is there any alternate way where the title is displayed as is? 是否有任何替代方式显示标题? I cannot use JQuery or any frameworks apart from pure and simple Javascript 我不能使用JQuery或任何框架,除了纯粹和简单的Javascript

Also note that this is not URL encoding but rather conversion of characters to their HTML entities. 另请注意,这不是URL编码,而是将字符转换为HTML实体。

You can use .textContent instead of .innerHTML . 您可以使用.textContent而不是.innerHTML

.innerHTML is actually not part of the DOM spec. .innerHTML实际上不是DOM规范的一部分。 Most browsers offer it as a non-standard feature. 大多数浏览器都将其作为非标准功能提供。 There are several problems with .innerHTML . .innerHTML有几个问题。

Since you're interested in the actual text value of the node you can use .textContent . 由于您对节点的实际文本值感兴趣,因此可以使用.textContent

It might be useful to know that the div abc actually has a node inside it, a TextNode , which holds the value. 知道div abc实际上有一个节点,一个TextNode来保存该值可能是有用的。 The div itself doesn't actually hold the value. div本身实际上并不具有价值。 If you were to traverse the DOM you would see this text node. 如果您要遍历DOM,您将看到此文本节点。

If your HTML is: 如果你的HTML是:

<div id="abc">Foo <strong>bar</strong> baz</div>

Your structure will be: 你的结构将是:

`div`:abc
  TextNode Foo
  `strong`
    TextNode bar
  TextNode baz

In this case abc.textContent will return "Foo bar baz" so it ignores the strong tag. 在这种情况下, abc.textContent将返回"Foo bar baz"因此它会忽略strong标记。

In comparison .innerHTML will return "Foo <strong>bar</strong> baz" 相比之下.innerHTML将返回"Foo <strong>bar</strong> baz"

Similarly for: 同样地:

<div id="abc">Foo&amp;Bar</div>

.textContent will be "Foo&Bar" , .innerHTML will be "Foo&amp;Bar" .textContent将是"Foo&Bar" ,. .innerHTML将是"Foo&amp;Bar"

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

相关问题 从div复制时防止复制html / css样式 - Prevent copying of html/css styles when copying from a div 如何防止页面和选项卡的 document.title 更改? - How do I prevent a page and tab's document.title from changing? 如何使用 jQuery/AJAX 在 HTML div 内加载 html 页面内容? - How do I load html page content inside of an HTML div using jQuery/AJAX? 虽然可见,但如何使此对话框阻止访问其余页面内容? - while visible how do i make this dialog prevent access to rest of the page content? 如何防止文本和/或页面内容双击html和javascript中的突出显示? - How do I prevent text and/or page content double-click highlighting in html and javascript? 如何将html页面中的div内容加载到主页上的div中? - How can I load div content from html pages into a div on main page? 如何防止弹出菜单 div 取代其他内容 div? - how do I prevent a popout menu div from displacing other content divs? 如何在将变量作为内容传递时使用jQuery将HTML元素添加到div中? - How do I add an HTML element into a div using jQuery while passing variables as content? 如何使用 JavaScript 更新/更改 HTML 内容并防止页面刷新? - How to update/change HTML content with JavaScript and prevent the page from refreshing? 如何更改Docco生成的HTML页面的标题? - How do I change the title of an HTML page generated by Docco?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM