简体   繁体   English

如何在jsp页面中使用javascript创建html对象

[英]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.

相关问题 如何使用JSP创建Tabbed Html页面 - How to create Tabbed Html page using JSP 如何使用ceratin Javascript逻辑更新JSP页面html元素? - How to update a JSP page html element using ceratin Javascript logic? 如何使用javascript / jquery将xml文件加载到jsp / html页面 - how to load xml file to jsp/html page using javascript/jquery 如何在jsp页面中包含javascript - How to include a javascript in a jsp page 如何在JavaScript中动态创建HTML页面 - How to create an HTML page dynamically in JavaScript 如何使用属性名称中的多个单词解析/解析 JSON 对象 - JavaScript - How to resolve/parse JSON obejct with multiple word in property name - JavaScript 如何将使用javascript动态创建的HTML表发送到另一个页面(servlet,jsp等)? - How to send an HTML table,which is created dynamically using javascript,to a another page(servlet,jsp etc)? 从html / jsp页面,如何访问firefox扩展名中的Javascript文件? - From the html/jsp page, how to access Javascript file which is in the firefox extension? 如何将EL表达式嵌入到由jquery / javascript生成的HTML字符串中以插入到JSP页面中? - How can an EL expression be embedded in an HTML string that is generated by jquery/javascript for insertion into a JSP page)? 在HTML / Javascript中创建页面导航 - Create page navigation in HTML/Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM