阅读:2263回复:5
[扩展开发] 如何动态添加控件
比如,我要用 javascript 把一个 checkbox 添加的我自己建的一个工具条中,该怎么写?
最好有示例代码。 谢谢! |
|
1楼#
发布于:2012-09-10 17:31
<!DOCTYPE HTML> <html><head> <meta charset="UTF-8"> <title>Dynamic Chrome Operation Sample</title> <script type="text/javascript"> function addACheckboxOrSomethingElseToTargetById(id){ var target=document.getElementById(id); var cb = document.createElement('input'); cb.type = 'checkbox'; cb.title = '[' + Math.random() + ']'; target.appendChild(cb); } </script> </head> <body> <button onclick="addACheckboxOrSomethingElseToTargetById('ImEmptyExceptForSomeJunk')">Add more junk</button> <div id=ImEmptyExceptForSomeJunk >Communist Party of China</div> </body></html> |
|
|
2楼#
发布于:2012-09-10 17:31
|
|
3楼#
发布于:2012-09-10 17:31
白左:<!DOCTYPE HTML> <html><head> <meta charset="UTF-8"> <title>Dynamic Chrome Operation Sample</title> <script type="text/javascript"> function addACheckboxOrSomethingElseToTargetById(id){ var target=document.getElementById(id); var cb = document.createElement('input'); cb.type = 'checkbox'; cb.title = '[' + Math.random() + ']'; target.appendChild(cb); } </script> </head> <body> <button onclick="addACheckboxOrSomethingElseToTargetById('ImEmptyExceptForSomeJunk')">Add more junk</button> <div id=ImEmptyExceptForSomeJunk >Communist Party of China</div> </body></html>回到原帖 非常感谢! 原来就是跟操作 HTML 一样的,我还以为操作 XUL 会有什么不同呢。 我试试。 对了,我在 ECMA 里见到一个 Node,不知道干什么用的。用法大概是: var checkbox = new Node(); checkbox.nodeName = "checkbox"; checkbox.label = "Check"; boxes.appendChild( checkbox ); |
|
4楼#
发布于:2012-09-10 17:31
白左:<!DOCTYPE HTML> <html><head> <meta charset="UTF-8"> <title>Dynamic Chrome Operation Sample</title> <script type="text/javascript"> function addACheckboxOrSomethingElseToTargetById(id){ var target=document.getElementById(id); var cb = document.createElement('input'); cb.type = 'checkbox'; cb.title = '[' + Math.random() + ']'; target.appendChild(cb); } </script> </head> <body> <button onclick="addACheckboxOrSomethingElseToTargetById('ImEmptyExceptForSomeJunk')">Add more junk</button> <div id=ImEmptyExceptForSomeJunk >Communist Party of China</div> </body></html>回到原帖 我试了一下,因为是 XUL 嘛,稍微改了一下: var checkbox = document.createElement( "checkbox" ); checkbox.style.listStyleImage = 'url( "..." )'; sitesBox.appendChild( checkbox ); // 以上的起作用了,但是以下的都不起作用。 checkbox.id = "..."; checkbox.src = "..."; checkbox.title = "..."; checkbox.label = "..."; checkbox.tooltiptext = "..."; checkbox.oncommand = "...;"; 这个问题看起来是动态添加的问题,如果直接写在 XUL 里是没有问题的。 |
|
5楼#
发布于:2012-09-10 17:31
改成这样就行了: checkbox.setAttribute( "id", "..." ); checkbox.setAttribute( "tooltiptext", "..." ); checkbox.setAttribute( "oncommand", "..." ); |
|