【问题标题】:Why the function is not read when I call it?为什么我调用该函数时不读取该函数?
【发布时间】:2020-10-22 21:09:02
【问题描述】:

我是法国人(对不起我的英语)是 Web 开发的新手,甚至更多的是 Javascript..

我创建了一个名为“ma​​nyStyles”的函数。

这个函数必须用 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 ..");

-我在括号内声明我的函数 ma​​nyStyles 有 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


【解决方案1】:

您使用let grabbedElement 在内部创建了一个变量,您为它分配了抓取的元素,但您还没有使用它。应该是这样,改变抓取的元素。

grabbedElement.style=`height:${elem_height}px;width:${elem_width}px;background-color:${element_bg_color};`;

您试图从document.body.grabbedElement 获取styles,但document.body.grabbedElement 不存在。

【讨论】:

  • 谢谢兄弟,它正在工作.. 只是我在使用 VS Code 时遇到了一些问题,他对这些东西进行了奇怪的着色.. 无论如何,谢谢 ;)
【解决方案2】:

首先看看你在做什么是创建一个变量,但你不使用这个变量来设置样式。 document.body.grabbedElement 等于 undefined

function manyStyles(elementGrab,elem_height,elem_width,element_bg_color){
   let grabbedElement=document.body.querySelector(`${elementGrab}`);
   grabbedElement.style=`height:${elem_height}px;width:${elem_width}px;background-color:${element_bg_color};`;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2022-12-10
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多