0

I'm building <div> elements using AJAX, and I want to add ZeroClipboard functionality. Firebug shows the code is building correctly, and when I copy it into a raw HTML test page it works too. The builds are not happening at onload, but down the track.

The code is as follows, calling some functions that create the new elements:

dom_append_child_with_onclick ("img",export_id,"icon_active",report_heading_id, "event.cancelBubble = true;");
dom_append_child ("div",export_script_id,"",report_heading_id);
text = "<script language='JavaScript'>var clip" +rnum +"=new ZeroClipboard.Client();clip"+rnum+".setText('');clip"+rnum+".addEventListener('mouseDown',function(client){alert('firing');clip"+rnum+".setText(document.getElementById('SL40').value);});clip"+rnum+".glue('XR"+rnum+"','RH"+rnum+"');</script>";
document.getElementById(export_script_id).innerHTML=text;

My question: when you insert a script into the <body>, do you have to do something to get it to fire? The script appears not to be doing its thing, and I can't get the alert 'firing' to display.

Note: the cancelBubble is to stop the onClick function of the underlying element. It may be unnecessary if I can get the flash working.

Thanks.

4

2 回答 2

1

您可以将脚本作为 DOM 对象注入页面,但这不适用于所有浏览器:

var s = document.createElement("script");
s.type = "text/javascript";
s.innerText = "var clip" +rnum +"=new ZeroClipboard.Client();clip"+rnum+".setText('');clip"+rnum+".addEventListener('mouseDown',function(client){alert('firing');clip"+rnum+".setText(document.getElementById('SL40').value);});clip"+rnum+".glue('XR"+rnum+"','RH"+rnum+"');";
document.getElementsByTagName("head")[0].appendChild(s);

或者,为了更好的兼容性,您可能只想声明一个在您的页面中设置它的函数,然后只需使用rnum作为参数调用该函数。

例如

function useZeroClipboard(rnum) {
    window["clip" + rnum] = new ZeroClipboard.Client();
    cwindow["clip" + rnum].setText('');
    window["clip" + rnum].addEventListener('mouseDown', function(client){
        alert('firing');
        window["clip" + rnum].setText(document.getElementById('SL40').value);
    });
    window["clip" + rnum].glue('XR"+rnum+"','RH"+rnum+"');
}

然后你可以在你的代码中调用它:

useZeroClipboard(rnum);

而不是编写脚本块。

于 2012-02-27T08:16:42.133 回答
0

这是一种将所有脚本递归替换为可执行脚本的方法:

function replaceScriptsRecurse(node) {                                                      
        if ( nodeScriptIs(node) ) {                                                         
                var script  = document.createElement("script");                             
                script.text = node.innerHTML;                                               

                node.parentNode.replaceChild(script, node);                                 
        }                                                                                   
        else {                                                                              
                var i        = 0;                                                           
                var children = node.childNodes;                                             
                while ( i < children.length) {                                              
                        replaceScriptsRecurse( children[i] );                               
                        i++;                                                                
                }                                                                           
        }                                                                                   

        return node;                                                                        
}                                                                                           
function nodeScriptIs(node) {                                                               
        return node.getAttribute && node.getAttribute("type") == "text/javascript";         
}                                                                                                                                                                                                                                                                         
于 2013-12-14T14:21:59.100 回答