[英]How to create a html obejct with javascript in a jsp page
all, I want to create a html obejct with javascript in a jsp page, but 'alert(GameObject1.loginurl);' 所有,我想在一个jsp页面中创建一个带有javascript的html对象,但是要'alert(GameObject1.loginurl);' will alert 'undefined'.
将提醒“未定义”。
Do I get some misstakes in the code below? 我在下面的代码中会误会吗? It seems that 'obj.appendChild' has failed.But Why?
看来'obj.appendChild'失败了,但是为什么呢?
var obj;
try {
obj = document.createElement("object");
obj.id = "GameObject1";
obj.name = "JavaGameObject1";
obj.setAttribute("classid", "clsid:72E6F181-D1B0-4C22-B0D7-4A0740EEAEF5");
obj.width = 640;
obj.height = 526;
var loginurl = document.createElement("param");
loginurl.setAttribute("name", "loginurl");
loginurl.setAttribute("value", "xx.xx.xx.xx:8080");
obj.appendChild(loginurl);
document.body.appendChild(obj);
alert(GameObject1.loginurl);
} catch (e) {
alert("Exception:" + e.message);
}
Based on your code, GameObject1
is never defined. 根据您的代码,从未定义
GameObject1
。 I think you are wanting to use obj
instead as that is the HTML object for the ID GameObject1
. 我认为您想使用
obj
代替,因为那是ID GameObject1
的HTML对象。
I will note however that even obj.loginurl
will still be undefined as you created the child HTML object param
called loginurl
, not a property to the HTML object obj
. 不过,我会注意,即使
obj.loginurl
为您创建的子HTML对象仍然是不确定param
称为loginurl
,而不是一个属性的HTML对象obj
。 (ie. You would have needed to do obj.loginurl = "xx.xx.xx.xx:8080"
to access it the way you seem to want) (即,您可能需要执行
obj.loginurl = "xx.xx.xx.xx:8080"
才能以您希望的方式对其进行访问)
To get the child element param
's value, you would want something like obj.children[0].value
which will return the value you set on your loginurl
object. 要获取子元素
param
的值,您需要使用obj.children[0].value
类的东西,它将返回您在loginurl
对象上设置的值。 Alternatively, in your current scope you could just call loginurl.value
. 或者,在您当前的作用域中,您可以仅调用
loginurl.value
。
When accessing child elements via obj.children[#]
, it is best to do checks to see if the element at that position exists so you don't throw exceptions everywhere. 通过
obj.children[#]
访问子元素时,最好进行检查以查看该位置上的元素是否存在,以便您不会在任何地方抛出异常。
To access the loginurl, you could use 要访问登录URL,可以使用
alert(obj.getElementsByTagName("param")[0]);
alert(obj.getElementsByTagName("param")[0].name);
alert(obj.getElementsByTagName("param")[0].value);
Accessing like the way you mentioned is proper way of getting the attributes and not the child element. 像您提到的那样访问是获取属性而不是子元素的正确方法。
GameObject1 // Will look for Object variable not the object with id "GameObject1" - You should use document.getElementById("GameObject1")
GameObject1.loginurl // will look for attribute and not the child element
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.