Javascript如何使函数在其他函数上工作
我刚刚开始学习JavaScript,想尝试一些东西Javascript如何使函数在其他函数上工作,javascript,html,Javascript,Html,我刚刚开始学习JavaScript,想尝试一些东西 如何使JavaScript函数在其他函数上工作?比如,我想当你点击一个div时,另一个div的背景色是:黄色(或JS中的x.style.backgroundColor=“yellow”),如下所示: <div onclick="yellow">Click me to paint the other div in yellow</div> <div id="painted">I should be painte
如何使JavaScript函数在其他函数上工作?比如,我想当你点击一个div时,另一个div的背景色是:黄色代码>(或JS中的
x.style.backgroundColor=“yellow”
),如下所示:
<div onclick="yellow">Click me to paint the other div in yellow</div>
<div id="painted">I should be painted yellow if you click the above div!</div>
单击我将另一个div绘制为黄色
如果你点击上面的div,我应该被涂成黄色!
在C语言中,我知道调用递归是可能的,所以我尝试使用它,尽管我不知道。当然,它没有成功,但是如果你想看的话,还有一个问题。
问:我如何使用JavaScript用其他div函数在黄色背景中绘制div?
提前谢谢 您应该拥有
document.getElementById(“背景变化”)代码>而不是getElementById(“背景变化”)代码>并且它会工作:)您应该拥有document.getElementById(“backgroundChange”)代码>而不是getElementById(“背景变化”)代码>和它将工作:)您将必须附加带有括号的函数,如fillYellow()
单击我将另一个div绘制为黄色
如果你点击上面的div,我应该被涂成黄色!
函数fillYellow(){
document.getElementById('painted').style.background=“黄色”;
}
在该函数内部,通过id绘制div,并为该div应用背景色。您必须使用括号附加函数,如fillYellow()
单击我将另一个div绘制为黄色
如果你点击上面的div,我应该被涂成黄色!
函数fillYellow(){
document.getElementById('painted').style.background=“黄色”;
}
在该函数中,通过id获取绘制的div,并为该div应用背景色。您的黄色事件处理程序应该查找id为“绘制”的div,然后向其添加一个类,该类将为其提供黄色文本
这是一种方法(一种简单的方法),但还有许多其他类似的方法可以实现这一点。您的黄色事件处理程序应该查找id为“painted”的div,然后向其添加一个类,该类将为其提供黄色文本
这是一种方法(简单的方法),但还有许多其他类似的方法可以实现这一点。因为你的问题是“如何让JavaScript函数在其他函数上工作?”我开始想,也许你真的在说做一些事件驱动的事情
P>因此,为什么不考虑这个(稍微更先进的)解决方案
当您单击一个
时,您会在每个
触发一个自定义“绘制”事件。
- 在自身激发的事件不同于在其他
s激发的事件
当
节点
看到“绘制”事件e
时,它将e.style
应用于节点.style
代码如下所示
function paintEvent(style) { // function make a custom event
var ev = new Event('paint');
ev.style = style;
return ev;
}
function applyPaint(node, style) { // function to style a node
var key;
for (key in style) { // loop over object
node.style[key] = style[key];
}
}
function paintHandler(e) { // function to fire when you see a "paint" event
applyPaint(this, e.style);
}
function clickHandler(e) { // function to fire when you see a "click" event
var divs = document.getElementsByTagName('div'),
i,
paint = paintEvent({'background-color': 'yellow'});
this.dispatchEvent(paintEvent({'background-color': 'red'}));
for (i = 0; i < divs.length; ++i) { // loop over array-like
if (divs[i] !== this)
divs[i].dispatchEvent(paint);
}
}
window.addEventListener('load', function () { // when the Window has loaded
var divs = document.getElementsByTagName('div'),
i;
for (i = 0; i < divs.length; ++i) { // loop over array-like
divs[i].addEventListener('click', clickHandler); // attach the handlers
divs[i].addEventListener('paint', paintHandler);
}
});
函数paintEvent(style){//函数生成自定义事件
var ev=新事件(“油漆”);
ev.style=风格;
返回ev;
}
函数applyPaint(节点,样式){//用于设置节点样式的函数
var键;
对于(键入样式){//对象上的循环
node.style[key]=style[key];
}
}
函数paintHandler(e){//看到“绘制”事件时要激发的函数
苹果漆(这种,即风格);
}
函数clickHandler(e){//当您看到“单击”事件时要激发的函数
var divs=document.getElementsByTagName('div'),
我
paint=paintEvent({'background-color':'yellow'});
dispatchEvent(paintEvent({'background-color':'red'}));
对于(i=0;i
因为你的问题是“我怎样才能让JavaScript函数在其他函数上工作?”我开始想也许你真的在说做一些事件驱动的事情
P>因此,为什么不考虑这个(稍微更先进的)解决方案
当您单击一个
时,您会在每个
触发一个自定义“绘制”事件。
- 在自身激发的事件不同于在其他
s激发的事件
当
节点
看到“绘制”事件e
时,它将e.style
应用于节点.style
代码如下所示
function paintEvent(style) { // function make a custom event
var ev = new Event('paint');
ev.style = style;
return ev;
}
function applyPaint(node, style) { // function to style a node
var key;
for (key in style) { // loop over object
node.style[key] = style[key];
}
}
function paintHandler(e) { // function to fire when you see a "paint" event
applyPaint(this, e.style);
}
function clickHandler(e) { // function to fire when you see a "click" event
var divs = document.getElementsByTagName('div'),
i,
paint = paintEvent({'background-color': 'yellow'});
this.dispatchEvent(paintEvent({'background-color': 'red'}));
for (i = 0; i < divs.length; ++i) { // loop over array-like
if (divs[i] !== this)
divs[i].dispatchEvent(paint);
}
}
window.addEventListener('load', function () { // when the Window has loaded
var divs = document.getElementsByTagName('div'),
i;
for (i = 0; i < divs.length; ++i) { // loop over array-like
divs[i].addEventListener('click', clickHandler); // attach the handlers
divs[i].addEventListener('paint', paintHandler);
}
});
函数paintEvent(style){//函数生成自定义事件
var ev=新事件(“油漆”);
ev.style=风格;
返回ev;
}
函数applyPaint(节点,样式){//用于设置节点样式的函数
var键;
对于(键入样式){//对象上的循环
node.style[key]=style[key];
}
}
函数paintHandler(e){//看到“绘制”事件时要激发的函数
苹果漆(这种,即风格);
}
函数clickHandler(e){//当您看到“单击”事件时要激发的函数
var divs=document.getElementsByTagName('div'),
我
paint=paintEvent({'background-color':'yellow'});
dispatchEvent(paintEvent({'background-color':'red'}));
对于(i=0;i
谢谢但我