HTML onload - 使用变量作为参数
我想使用类似的东西:
<body onLoad="init('A sentence with "quoted text" as parameter')">
不幸的是,这确实有效,因为参数中的引号没有得到正确处理。
转义引号也不起作用
<body onLoad="init('A sentence with \"quoted text\" as parameter')">
(上面也不起作用)。
我该如何处理这个问题。我想也许我可以创建一个字符串变量并将我的句子(带引号)分配给它。但我不知道该怎么做!加载正文是 HTML,Javascript 变量仅在脚本范围内可见,对吧?准确地说,以下内容不起作用:
<script language="JavaScript">
var dada='A sentence with \"quoted text\" as parameter';
</script>
<body onLoad="init($dada, '</a>')">
I want to use something like:
<body onLoad="init('A sentence with "quoted text" as parameter')">
Unfortunately, this does work, as the quotes in the parameter are not treated properly.
Escaping the quotes also does not work
<body onLoad="init('A sentence with \"quoted text\" as parameter')">
(Above also does not work).
How do I deal with this. I though maybe I can create a string variable and assigne my sentence (with quotes) to it. But I dont know how to do it! The body onload is HTML and the Javascript variables would be visible only within the scope of the script, right? To be precise, the following does not work:
<script language="JavaScript">
var dada='A sentence with \"quoted text\" as parameter';
</script>
<body onLoad="init($dada, '</a>')">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用 HTML 实体才能使其工作:
不过,更简洁的方法是在文档头部的单独
部分中分配值。
onload
事件有一个普遍的缺点,即仅当文档及其所有资源(图像、样式表...)已加载时才会触发。这就是 onDOMLoad 事件的用武之地:当核心 HTML 结构准备就绪并且所有元素都已渲染时,它会触发。不过,它并没有得到跨浏览器的统一支持,因此所有框架都有自己的实现。jQuery 版本称为
.ready()
。You would have to use HTML entities to make it work:
the much cleaner way, though, would be to assign the value in a separate
<SCRIPT>
part in the document's head.the
onload
event has the general downside that it is fired only when the document and all its assets (images, style sheets...) have been loaded. This is where the onDOMLoad event comes in: It fires when the core HTML structure is ready, and all elements are rendered. It is not uniformly supported across browsers, though, so all frameworks have their own implementation of it.The jQuery version is called
.ready()
.为什么不以编程方式设置它,而不是内联
onLoad
方法。然后,您可以使用 闭包 的强大功能将数据获取到函数中。Rather than inlining the
onLoad
method, why not set it programatically. You can then use the power of closures to get the data into the function.这不是最好的方式
not the nicest way
顺便说一句,我犯了一个愚蠢的错误。
这也有效。我在 onload 调用中使用 $dada 而不是 dada 。
By the way, I was making a silly mistake.
This works too. I was using $dada instead of dada in the onload call.