Body OnClick不起作用......但onclick在附加到< a>时工作。标签? [英] Body OnClick doesn't work... but the onclick works when attached to a <a> tag?
本文介绍了Body OnClick不起作用......但onclick在附加到< a>时工作。标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Onclick事件
I have the following Onclick event
onClick="document.body.style.background = 'url(galleries/g2.jpg)'"
当我将其附加到
<a onClick="document.body.style.background = 'url(galleries/g2.jpg)'"> blah</a>
一切正常。但我希望它作为一个身体onclick事件如下
all works fine. but I want it as a body onclick event as follows
<body class="g" onClick="document.body.style.background = 'url(galleries/g2.jpg)'">
我没有骰子。
唯一要注意的是g类已经指定了背景图像,但我认为这不重要。
The only thing to note would be that the class "g" already has a background image specified, but I don't think that should matter.
推荐答案
使用 this。
喜欢这个
<body onClick="this.style.background = 'url(http://placehold.it/20x50)';">
上面代码的JSFiddle jsfiddle.net/mrRRG/2/ 。
但是请不要使用这种类型的代码构造。最好制作一些CSS类并使用javascript在事件触发时更改它们
JSFiddle of the code above jsfiddle.net/mrRRG/2/. But please do not use this type of code construction. It's better to make few CSS classes and use javascript to change them on event fire
这篇关于Body OnClick不起作用......但onclick在附加到< a>时工作。标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文