HTML onload - 使用变量作为参数

发布于 2024-09-29 10:28:52 字数 588 浏览 4 评论 0原文

我想使用类似的东西:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月流歌 2024-10-06 10:28:52

您必须使用 HTML 实体才能使其工作:

<body onLoad="init('A sentence with "quoted text" as parameter')">

不过,更简洁的方法是在文档头部的单独

...
<script>
body.onload = function() { init('A sentence with "quoted text" as parameter'); }
</script>
<body>
...

onload 事件有一个普遍的缺点,即仅当文档及其所有资源(图像、样式表...)已加载时才会触发。这就是 onDOMLoad 事件的用武之地:当核心 HTML 结构准备就绪并且所有元素都已渲染时,它会触发。不过,它并没有得到跨浏览器的统一支持,因此所有框架都有自己的实现。

jQuery 版本称为 .ready()

You would have to use HTML entities to make it work:

<body onLoad="init('A sentence with "quoted text" as parameter')">

the much cleaner way, though, would be to assign the value in a separate <SCRIPT> part in the document's head.

...
<script>
body.onload = function() { init('A sentence with "quoted text" as parameter'); }
</script>
<body>
...

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().

℡Ms空城旧梦 2024-10-06 10:28:52

为什么不以编程方式设置它,而不是内联 onLoad 方法。然后,您可以使用 闭包 的强大功能将数据获取到函数中。

<script type="text/javascript">
var data = "some data";
document.body.onload = function()
{
    alert(data);
}
</script>

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.

<script type="text/javascript">
var data = "some data";
document.body.onload = function()
{
    alert(data);
}
</script>
ぃ双果 2024-10-06 10:28:52

这不是最好的方式

onload='init("A sentence with \"quoted text\" as parameter")'

not the nicest way

onload='init("A sentence with \"quoted text\" as parameter")'
〃安静 2024-10-06 10:28:52

顺便说一句,我犯了一个愚蠢的错误。

<script language="JavaScript"> 
var dada='A sentence with \"quoted text\" as parameter'; </script> 
<body onLoad="init(dada, '</a>')"> 

这也有效。我在 onload 调用中使用 $dada 而不是 dada 。

By the way, I was making a silly mistake.

<script language="JavaScript"> 
var dada='A sentence with \"quoted text\" as parameter'; </script> 
<body onLoad="init(dada, '</a>')"> 

This works too. I was using $dada instead of dada in the onload call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文