【发布时间】:2020-10-22 21:09:02
【问题描述】:
我是法国人(对不起我的英语)是 Web 开发的新手,甚至更多的是 Javascript..
我创建了一个名为“manyStyles”的函数。
这个函数必须用 querySelector("") 抓取一个元素,然后对之前抓取的元素应用一些样式。非常简单的功能,只是为了试用和学习。
脚本的逻辑是:
-我创建了 4 个变量,其中包含 function prompt(""); => 用户的价值存储在这 4 个变量中。
let askForElement= prompt("Type some selectors, with CSS synthax.. example:h1,h2.. #test(id) or .test(class)");
let askingHeight= prompt("Type some value for the height");
let askingWidth= prompt("Type some value for the width");
let askingBackgroundColor= prompt("Type some value for the background-color ex:red,green,blue, #66a ..");
-我在括号内声明我的函数 manyStyles 有 4 个参数(变量) (elementGrab,elem_height,elem_width,element_bg_color) => elementGrab 用于捕获我之前解释的元素和其他元素参数是样式的变量。
function manyStyles(elementGrab,elem_height,elem_width,element_bg_color){
let grabbedElement;
grabbedElement=document.body.querySelector(`${elementGrab}`);
document.body.grabbedElement.style=`height:${elem_height}px;width:${elem_width}px;background-color:${element_bg_color};`;
}
=>在 body 函数中,我只是创建了一个名为“grabbedElement”的变量,用于在其中存储要在以下语句中抓取的元素。
最后我用她的名字调用函数,并在参数中输入 4 个变量提示(在第一步中创建);
manyStyles(askForElement,askingHeight,askingWidth,askingBackgroundColor);
通常该函数必须使用 querySelector 抓取元素,然后对其应用样式。
但由于某种原因不起作用
看完整的代码解释的比较多..
谢谢
let askForElement= prompt("Type some selectors, with CSS synthax.. example:h1,h2.. #test(id) or .test(class)");
let askingHeight= prompt("Type some value for the height");
let askingWidth= prompt("Type some value for the width");
let askingBackgroundColor= prompt("Type some value for the background-color ex:red,green,blue, #66a ..");
function manyStyles(elementGrab,elem_height,elem_width,element_bg_color){
let grabbedElement;
grabbedElement=document.body.querySelector(`${elementGrab}`);
document.body.grabbedElement.style=`height:${elem_height}px;width:${elem_width}px;background-color:${element_bg_color};`;
}
manyStyles(askForElement,askingHeight,askingWidth,askingBackgroundColor);
【问题讨论】:
-
您已经解释了一切,但实际问题除外(没有 “但由于某种原因它不起作用” 不是一个充分的问题/错误描述)。会发生什么而不是预期的行为?控制台有错误吗?
-
我在调用函数时 console.log 一行,控制台写: => Uncaught TypeError: Cannot set property 'style' of undefined
标签: javascript html function parameters prompt