我如何创建可互换的说明?

编程入门 行业动态 更新时间:2024-10-28 14:29:24
本文介绍了我如何创建可互换的说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

document.body.style.backgroundColor = #F00;

此声明将正文背景色设为红色。

在我的原始代码中,我有一个函数,通过标记名称将元素分配给全局变量元素。我在调用这个函数后发现我可以这样做: element.style.backgroundColor =#F00;

<但是,似乎没有任何方法可以替代语句的属性部分。下面我制作了最明显的方法,尽管我也尝试将参数分配给变量和各种引用组合。

function assignStyle(value){document.body.style.backgroundColor = value;}函数attemptStyle(property,value){document.body.style.property = value;}

button {font-family:monospace; }

< button onmouseover =assignStyle('#131519 );的onmouseout =assignStyle(#FFF);>函数assignStyle(value){}< / button>< br />< button onmouseover =attemptStyle('backgroundColor','#137619'); onmouseout =attemptStyle('backgroundColor','#FFF');> function attemptStyle(property,value){}< / button>

编辑:这个问题不是如何将一个属性添加到使用变量的JavaScript对象中名字?。 这个问题包含了jQuery,包括我自己在内的许多人完全不知道。我的问题是关于基本的JavaScript。此外,我想知道如何将一个属性添加到一个元素,并通过将参数传递给一个函数参数。就我而言,变量会使问题进一步复杂化。

解决方案

我不太确定你的意思替换 backgroundColor ,但我会猜测你试图以编程方式访问 backgroundColor 属性。如果是这样,你的第二个函数需要以下修改: pre $ 函数attemptStyle(property,value){ document.body .style [属性] =值; $ b

因此,如果您调用 attemptStyle('backgroundColor' ,'red'),您将使文档的背景颜色为红色。

document.body.style.backgroundColor="#F00";

This statement styles the body background color as red.

In my original code I have a function that assigns elements by tag name to a global variable element. I've found after calling this function I can do this: element.style.backgroundColor="#F00";

However, there doesn't seem to be ANY way to substitute for the property section of the statement. Below I've produced the most obvious approach, though I have also tried assigning the parameters to variables and various combinations of quotation.

function assignStyle(value) { document.body.style.backgroundColor = value; } function attemptStyle(property,value) { document.body.style.property = value; }

button { font-family: monospace; }

<button onmouseover="assignStyle('#131519');" onmouseout="assignStyle('#FFF');"> function assignStyle(value){} </button><br /> <button onmouseover="attemptStyle('backgroundColor','#137619');" onmouseout="attemptStyle('backgroundColor','#FFF');"> function attemptStyle(property,value){} </button>

Edit: This question is not a duplicate of How do I add a property to a JavaScript object using a variable as the name?. That question contains jQuery, which many people, including myself, know literally nothing about. My question is concerned with basic JavaScript. Also, I want to know how to add a property to an element, and so by means of passing an argument to a function parameter. As far as I'm concerned, variables complicate the question further.

解决方案

I'm not too certain of what you mean by "substituting" backgroundColor, but I'll wager a guess that you're trying to access the backgroundColor property programmatically. If so, your second function needs the following amendment:

function attemptStyle(property,value){ document.body.style[property]=value; }

Thus, if you call attemptStyle('backgroundColor', 'red'), you will make the background color of the document red.

更多推荐

我如何创建可互换的说明?

本文发布于:2023-10-07 20:02:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!