2015-02-11 33 views
0

我所試圖做的是從一個文本框獲得的價值,並將其裝入容器中,像這樣:添加腳本DOM,在文本框用戶插入

var script = $('#txtbox').val(); 
$('.container').html(script); 

我也嘗試創建的腳本再次通過查找一個用戶插入的值,並創建一個新的,並將它附加到容器中,以便JavaScript的:

var tempdiv = $("<div></div>").html($('#txtbox').val()); 
$(tempdiv).find('script').each(function(index,$element){ 
    var script = document.createElement("script"); 
    $.each($element.attributes, function(){ 
     if (this.specified){ 
      script.setAttribute(this.name, this.value); 
     } 
    }); 
    script.innerHTML = $($element).html(); 
    $('.container').append(script); 
}); 

,但仍然無法正常工作,我得到:

未能在「文檔」執行「寫」:這是不可能寫入一個文件從異步加載外部腳本

有什麼建議?也許嘗試在iframe中加載腳本,以某種方式刷新DOM或什麼?

回答