[英]How to get text from textfield and pass it as a parameter to a javascript function
[英]How to get a parameter form a javascript function as text
<div id="demo" onclick="applycss(250,500,"red")"></div>
<script>
function applycss(){
var box = document.getElementById("this.id").style;
box.height = fisrt parameter;
box.width = second parameter;
box.backgroundColor = third parameter;
}
</script>
這個怎么做? 我想將值250, 500, red
到div上的div上,這樣我就可以對任何元素使用此函數而無需進行長時間編碼。
嘗試這個
<script>
function applycss(fisrt,second,third){
var box = document.getElementById("demo").style;
box.height=fisrt;
box.width= second;
box.backgroundColor= third;
}
</script>
您有一些語法錯誤,請參閱更新的代碼。 對於多個CSS屬性,您需要創建參數值的css-string並將其設置如下:
HTML:
<div id="demo" onclick="applycss(this, '250', '500', 'red');">Div to apply CSS</div>
JS:
function applycss(obj, height, width, bgColor){
var cssStyle = "height: " + height + "px; ";
cssStyle += "width: " + width + "px; ";
cssStyle += "background-color: " + bgColor + ";";
if(typeof(obj.style.cssText) != 'undefined')
obj.style.cssText = cssStyle;
else
obj.setAttribute('style', cssStyle);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.