使用 jQuery 将 Textarea 文本设置为格式正确的 HTML 代码
我正在尝试通过 Javascript (jQuery) 为我的 Textarea 生成文本,因为我在 JS 中有一个变量,我需要将其包含在其中。这就是我现在所做的:
$("#txt_banner1").text(
'<a href="'+producturl+'"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125"><param name="movie" value="../../images/banerxsky2.swf" /><param name="quality" value="high" /><embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125"></embed></object>')
现在您可能会想:“伙计,格式化您的代码”。好吧,这就是问题 - 我希望我的文本区域包含格式正确的 HTML 代码,但是当我在参数中格式化我的代码时,它不会工作(脚本停止)。
有什么建议吗?
编辑:格式化代码,不起作用:
$("#txt_banner1").text(
'<a href="'+producturl+'">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125">
<param name="movie" value="../../images/banerxsky2.swf" />
<param name="quality" value="high" />
<embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125">
</embed>
</object>');
编辑2:这是我的文本区域代码:
<textarea id="txt_banner1"><a href="AFF_URL">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125">
<param name="movie" value="http://xSkySoftware.com//images/banerxsky2.swf" />
<param name="quality" value="high" />
<embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125"></embed>
</object>
</textarea>
I am trying to generate the text for my Textarea thru Javascript (jQuery), because I have a variable in JS I need to include in there. This is what I do now:
$("#txt_banner1").text(
'<a href="'+producturl+'"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125"><param name="movie" value="../../images/banerxsky2.swf" /><param name="quality" value="high" /><embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125"></embed></object>')
Now you are probably going to think: "Dude, format your code". well, thats the problem - I would like my textarea to contain my HTML code properly formated, however when I format my code in the parameters, it wont work (script stops).
Any suggestions?
EDIT: Formated code, which is not working:
$("#txt_banner1").text(
'<a href="'+producturl+'">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125">
<param name="movie" value="../../images/banerxsky2.swf" />
<param name="quality" value="high" />
<embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125">
</embed>
</object>');
EDIT2: Here is my Textarea code:
<textarea id="txt_banner1"><a href="AFF_URL">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="125" height="125">
<param name="movie" value="http://xSkySoftware.com//images/banerxsky2.swf" />
<param name="quality" value="high" />
<embed src="http://www.xSkySoftware.com/images/banerxsky2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="125" height="125"></embed>
</object>
</textarea>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
讽刺的答案是“你做错了”。别担心,这很容易解决。
JavaScript 不允许多行字符串。你可以写:
但不能
除非你转义换行符(这不是 ECMA 规范的一部分,但我测试过的所有浏览器都支持这一点)
处理多行字符串的“正确”方法是向字符串添加换行符,但是你可以用更多的引号来模拟换行符:
是的,它很糟糕,但是你已经有了它。
The snide answer is "You're doing it wrong". Don't worry, it's an easy fix.
JavaScript doesn't allow multiline strings. You can write:
but not
Unless you escape the newline char (this is not a part of the ECMA Spec, but all browsers I've tested support this)
The "correct" way to do multiline strings is to add newline characters to the string, but you can simulate the newline with some more quotes:
Yes, it sucks, but there you have it.
您可以预先设置 txt_banner1 的值,然后只需替换脚本中的producturl:
script:
You could set the value of your txt_banner1 beforehand, and the just replace the producturl in the script:
script:
您可以将截取的 HTML 放在页面上的隐藏容器中,并按照您喜欢的方式对其进行格式化:
样式:
标记:(
请注意您错过的结束
。)
然后,抓住它通过
innerHTML
,在大多数浏览器中,它会被标准化为紧凑的 HTML。如果在特定浏览器上没有,您仍然可以使用一些正则表达式替换来清理标记之间的空白。在 Chrome 上,您仍然需要修剪前导和尾随空白:然后您可以将其放置在文本区域中:
You can place your HTML snipped in a hidden container on your page and format it the way you like:
Style:
Markup:
(Note the closing
</a>
that you missed.)Then, grab it via
innerHTML
, which, in most browsers, gets normalized to compact HTML. If it doesn't on a particular browser, you can still use some regular expression replacement to clean up the white space between the tags. On Chrome, you still have to trim the leading and trailing white space:Then you can place this inside your textarea:
我建议您使用隐藏的
元素将链接标记放在页面本身中:
然后,您可以使用 jQuery 克隆链接,修改其
href 属性,并使用生成的标记来填充您的
编辑:稍微调整标记以获得更好的结果。 Fiddle 可用此处。
I would suggest you put the link markup in the page itself using, say, a hidden
<div>
element:Then, you can use jQuery to clone the link, modify its
href
attribute on the fly and use the resulting markup to populate your<textarea>
element:EDIT: Slightly tweaked markup for better results. Fiddle available here.