JavaScript中的代码不起作用 [英] code in javascript does not work

查看:85
本文介绍了JavaScript中的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript初学者

代码打开/关闭灯泡image.i需要知道bulbon的规则。

i认为它的问题。

i'm beginner in javascript
code turn on/off light bulb image.i need to know the rule of "bulbon".
i think its the problem.

<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
  element=document.getElementById('myimage') 
     if (element.src.match("bulbon"))
        {
          element.src="C:\Users\MOHAMED\Desktop\pic_bulboff.gif";
        }
     else
        {
          element.src="C:\Users\MOHAMED\Desktop\pic_bulbon.gif";
        }
}
</script>

<img id="myimage"  önclick="changeImage()"

src="C:\Users\MOHAMED\Desktop\pic_bulboff.gif" width="100" height="180">

<p>Click the light bulb to turn on/off the light</p>

</body>
</html>

推荐答案

Hi,

Please use the virtual path instead of the physical path. Add the images to project folder and refer them. Also Use IndexOf instead of match.

code
<pre lang="Javascript"> <script type="text/javascript">
        function changeImage() {
            element = document.getElementById('myimage')
            if (element.src.indexOf("pic_bulboff") > 0) {
                element.src = "pic_bulbon.gif";
            }
            else {
                element.src = "pic_bulboff.gif";
            }
        }
</script>
 </pre>
Hope it helps...


您的问题我需要知道规则bbon。





当您点击图片时,会调用javascript并且if条件正在检查您的图像已经点击了。



换句话说,如果(element.src.match(bulbon))条件检查你点击的图像是否得到名称字符串bulbob在它的名字上







这对我有用



Your question i need to know the rule of "bulbon".

"
When you click on the image, the javascript is invoked and the if condition is checking which image you have clicked on.

In other words if (element.src.match("bulbon")) condition checks whether the image you clicked got the name string "bulbob" on its name

"

This works for me

<html>
    <head>
        <title></title>
        <script>
            function changeImage() {
             
                element = document.getElementById('myimage')
                if (element.src.match("crm")) {
                   
                    element.src = "img/cal.png";
                }
                else {
                   
                    element.src = "img/crm.jpg";
                }
            }
</script>
    </head>
<body>
    
 
<img id="myimage"  önclick="changeImage();">
src="img/crm.jpg" height="100" width="100"/>
    
<p>Click the light bulb to turn on/off the light</p>
 
</img></body>
</html>


bulbon没什么特别的。所有魔法,完成了这里是通过javascript的match()函数执行的。请在下面的链接中详细解释match()



http://www.w3schools.com/jsref/jsref_matc h.asp [ ^ ]



这里的主要概念是找到特定单词的匹配,作为match()方法中的参数给出。在上面的示例中,document.getElementById('myimage')为您提供访问权限与id'myimage'有关的HTML对象。你已经将对象存储在一个名为'element'的变量中。现在你的下一行代码是element.src.match(bulbon)。它只是指src属性你的HTML标记中提供了img标签。在你的情况下,element.src最初的意思是C:\ Users \ MOHAMED \Desktop \ pic_bulboff.gif。要对该字符串添加匹配('bulbon')方法。这个方法。意味着检查你的字符串是否包含字符串'bulbon'。如果它包含,则返回true,否则返回false。



i认为你的字符串有问题你指定的图像文件的路径。其他的东西是合乎逻辑的,适合于你据我所知。请检查一下。希望这对你有所帮助。
There is nothing special in bulbon.All the magic,done over here is carried out by match() function of javascript.Refer detailed explanation of match() in below link

http://www.w3schools.com/jsref/jsref_match.asp[^]

Main concept over here is to find a match of particular word,given as an argument in match() method.In above example document.getElementById('myimage') gives you access of an HTML object pertaining to id 'myimage'.you have stored the object in a variable named 'element'.Now your next line of code says element.src.match("bulbon").It simply refers to the src attribute of img tag provided in your HTML markup.in your case element.src initially means "C:\Users\MOHAMED\Desktop\pic_bulboff.gif".To that string you are appending match('bulbon') method.This means to check whether your string contains a string 'bulbon' or not.If it contains, it returns true else it returns false.

i think there is some problem in your path of image file specified by you.Other things are logical and appropriate to the best of my knowledge.Please check it out.Hope this will help you.


这篇关于JavaScript中的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆