2014-05-18 61 views
0

所以我有一個對象,當我在導航中單擊時加載數據。加載特定數據時是否可以更改該對象的大小?我可以在Js中更改對象的大小嗎?

<object id="box" width="250" height="250" data=""></object> 

和js,它加載數據但不更改大小。

document.getElementById("banner").onclick = function(){ 
    document.getElementById("box") 
      .setAttribute("data", "sfw/danser.swf", 'height', 600, 'width', 150) 

} 

回答

4

你應該設置每個單獨的屬性,該setAttribute函數不接受超過2個參數:

.setAttribute("data", "sfw/danser.swf") 
.setAttribute('height', 600) 
.setAttribute('width', 150) 

https://developer.mozilla.org/en-US/docs/Web/API/Element.setAttribute

的jQuery的版本setAttribute功能,該attr方法,接受對象:

$("#banner").on('click', function(event) { 
    $("#box").attr({ 
     "data" : "sfw/danser.swf", 
     "height" : 600, 
     "width" : 150 
    }); 
}); 
+0

因爲它被標記爲[tag:jQuery],所以值得一提的是jQuery方式允許'$(「#box」)。attr({「data」:「swf/danser.swf」,「height」:「600px」 ,「width」; 150px「})' –

+0

@BenjaminGruenbaum是的,謝謝你提到它。 – undefined

相關問題