本文介绍了我如何创建可互换的说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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>

编辑:这个问题不是。
这个问题包含了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.

这篇关于我如何创建可互换的说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 21:12