简体   繁体   English

如何从JavaScript函数获取参数作为文本

[英]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>

How to do this? 这个怎么做? I want to apply value 250, 500, red to div on click so that I can use this function to any element without long coding. 我想将值250, 500, red到div上的div上,这样我就可以对任何元素使用此函数而无需进行长时间编码。

try this 尝试这个

<script>
     function applycss(fisrt,second,third){
        var box = document.getElementById("demo").style;
          box.height=fisrt;
          box.width=  second;
          box.backgroundColor= third;
}
 </script>

You have some syntax error, see updated code. 您有一些语法错误,请参阅更新的代码。 For multiple CSS properties, you need to create css-string of your parameter values and set it as following: 对于多个CSS属性,您需要创建参数值的css-string并将其设置如下:

HTML: HTML:

<div id="demo" onclick="applycss(this, '250', '500', 'red');">Div to apply CSS</div>

JS: 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);
}

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文本字段中获取文本并将其作为参数传递给javascript函数 - How to get text from textfield and pass it as a parameter to a javascript function 如何获取参数 function javascript - How to get parameter function javascript 使用javascript从表单参数获取值的Highlite文本 - Highlite text using javascript that get value from form parameter 如何将文本从表单传递到javascript函数 - How to pass text from form to javascript function 大文本作为javascript函数中的参数 - large text as parameter in javascript function 如何获取值用户输入的html文本表单字段传递给javascript函数? - How to get value user entered into html text form field passed to javascript function? 如何将form_id发送给javascript函数作为参数? - How can I send form_id to javascript function as a parameter? 单击复选框禁用/启用表单文本框,并将文本框的名称作为参数传递给javascript函数 - Disabling/Enabling of form text boxes on click of checkbox and passing name of the text box as a parameter to javascript function 如何在Javascript中的另一个函数中返回一个带有参数的函数 - How to get return of one function with parameter in other function in Javascript 将GET参数传递给JavaScript函数? - Pass a GET parameter to a JavaScript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM