简体   繁体   English

为什么在尝试使用javascript动态添加html时,我的html字符串中的字符丢失了?

[英]Why am I losing my characters from my html string when trying to add html dynamically using javascript

I have a page that I am trying to dynamically add some links to. 我有一个页面,我试图动态添加一些链接。 The links are getting added to the page fine, but the '[' and ']' at either end of the line are getting dropped. 链接可以很好地添加到页面上,但是该行任一端的'['和']'都可以删除。 The code from my .js file is: 我的.js文件中的代码是:

var html = "[ <a href='#'>Change</a>&nbsp;|&nbsp;<a href='#'>Remove </a> ]";
$(html).appendTo("#id123");

The result I want is: 我想要的结果是:

[ <a href='#'>Change</a>&nbsp;|&nbsp;<a href='#'>Remove</a> ]

The result I'm getting is: 我得到的结果是:

<a href='#'>Change</a>&nbsp;|&nbsp;<a href='#'>Remove</a>

If I wrap the line in a <span> tag like so: 如果我将行包装在<span>标记中,如下所示:

var html = "<span>[ <a href='#'>Change</a>&nbsp;|&nbsp;<a href='#'>Remove </a> ]</span>";
$(html).appendTo("#id123");

it renders as expected. 它呈现预期的效果。 I set a breakpoint on the code and checked the html var right before the .appendTo and it contains the '[' and ']'. 我在代码上设置了一个断点,并在.appendTo之前检查了html var,它包含了[[和']'。

Anyone know why this is happening? 有人知道为什么会这样吗? Are '[' and ']' special character that need escaped and I'm just forgetting that fact? 是否需要转义'['和']'特殊字符,而我只是忘记了这一事实?

When you wrap your html variable inside of "$()", it creates a jQuery object out of it. 当将html变量包装在“ $()”内时,它会从中创建一个jQuery对象。 That object removes anything that's outside of a markup tag (like <a> or <span>). 该对象将删除标记标签之外的所有内容(例如<a>或<span>)。 You can take out the"[" and put "TESTING THIS" in it's place and you'll see it still won't show up. 您可以取出“ [”并在其中放置“ TESTING THIS”,然后您仍然会看到它不显示。

So that's why you're losing it in your output. 这就是为什么您在输出中丢失它的原因。

No, those are just not valid XHTML. 不,那些只是无效的XHTML。 If you put the '[' and ']' in their own spans, like so: 如果将'['和']'放在自己的跨度中,如下所示:

 "<span>[&nbsp;</span><a href='#'>Change</a>&nbsp;|&nbsp;<a href='#'>Remove </a></span>&nbsp;]</span>"

You would also get your expected text. 您还将获得您期望的文本。 jQuery will parse and create valid HTML, and the brackets aren't contained in an element. jQuery将解析并创建有效的HTML,并且括号不会包含在元素中。

I'm not sure why this happens. 我不确定为什么会这样。 It seems like something internal to the appendTo() I tried it with append() and it worked out fine. 看起来好像是appendTo()内部的东西,我使用append()尝试了一下,效果很好。 So you could write $("#id123").append(html) 因此,您可以编写$("#id123").append(html)

暂无
暂无

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

相关问题 我正在尝试使用javascript函数更改html链接的样式。 为什么我的代码不起作用? - I am trying to change the style of a html link with javascript functions. Why does my code not work? 我正在尝试使用 javascript 将图像添加到 HTML,但没有内容? - I am trying to add an image to HTML using javascript but there is no content? 我正在尝试使用JavaScript在HTML中添加表 - I am trying to add table in HTML using JavaScript 为什么我不能动态地向HTML表添加行? - Why can't I add rows dynamically to my HTML table? 我正在尝试使用 join() function 从 JavaScript 数组中创建一个大字符串,如果可能的话,是否可以包含 html - I am trying to make a big string from a JavaScript Array using join() function is it possible to include html with that if possible 为什么在动态添加包含div的行时,我的HTML表列会重新调整大小? - Why do my HTML table columns resize when I dynamically add a row containing a div? 当我想使用 JavaScript 从 HTML 中获取字符串时,为什么会得到 Null - Why am I getting Null when I want to get a String from an HTML with JavaScript 如何动态添加脚本并运行它。 我可以将它添加到我的 html 代码中,但它没有被执行 - How do I add script dynamically and run it. I am able to add it to my html code, but it is not executed 当我使用 javascript 将我的 forms 提交给 Z466DEEC76ECDF5FCA6D48571F6324 时,Html 表单验证不起作用 - Html form validation does not work when i am using javascript to submit my forms to json 为什么当我在我的 aspx 页面中的 html 图像点击事件中调用 JavaScript 函数时,我的网页正在刷新? - why my webpage is refreshing when i am calling a JavaScript function in my aspx page on a html image click event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM