HTML onload - 使用变量作为参数 [英] HTML onload - using variables as parameters

查看:482
本文介绍了HTML onload - 使用变量作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < body onLoad =init('带引号文本的句子作为参数')> 

不幸的是,这确实奏效,因为参数中的引号处理不当。



转义引号也不起作用

 < body onLoad =init '带\的句子引用text \作为参数')> 

(以上也不起作用)。

我如何处理这个问题。我虽然也许我可以创建一个字符串变量,并将我的句子(带引号)分配给它。但我不知道该怎么做!正文onload是HTML和JavaScript变量将仅在脚本的范围内可见,对吧?准确地说,以下内容不起作用:

 < script language =JavaScript> 
var dada ='带\的句子引用text \作为参数';
< / script>
< body onLoad =init($ dada,'< / a>')>


解决方案

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

 < body onLoad =init('带有& quot; quoted text& quot;参数'的句子) > 

虽然更清晰的方法是将值分配给单独的< SCRIPT> 在文件头部分。

  ... 
< ;脚本>
body.onload = function(){init('带引号文本作为参数的句子); }
< / script>
< body>
...

onload 事件具有普遍的缺点,只有当文档及其所有资源(图像,样式表...)被加载时才会触发它。这就是onDOMLoad事件的来源:当核心HTML结构准备就绪时,它会触发,并且所有元素都被渲染。然而,跨浏览器的支持并不统一,所以所有的框架都有自己的实现。



jQuery版本被称为 .ready()


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>')">

解决方案

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

<body onLoad="init('A sentence with &quot;quoted text&quot; 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().

这篇关于HTML onload - 使用变量作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆