Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击链接以更改没有jQuery的图像的src_Javascript_Html - Fatal编程技术网

Javascript 单击链接以更改没有jQuery的图像的src

Javascript 单击链接以更改没有jQuery的图像的src,javascript,html,Javascript,Html,我想点击一个链接来改变图片的来源。以下是我到目前为止的想法: HTML: <a href = "#" id = "blabla" onclick = "changesrc(); return:false;">click</a> <div id = "bulb"> <center><img src = "C:\Users\hp\Desktop\on.gif" style = "width:180px;height:270px;posi

我想点击一个链接来改变图片的来源。以下是我到目前为止的想法:

HTML:

<a href = "#" id = "blabla" onclick = "changesrc(); return:false;">click</a> 
<div id = "bulb">
    <center><img src = "C:\Users\hp\Desktop\on.gif" style = "width:180px;height:270px;position:relative;top:25px;border:2px solid black;"></center>
</div>
我是javascript的初学者,请帮助我。根据我的说法,单击a(用文本单击)函数change src就会被执行。有一个可变的工作。工作通过id=灯泡调用元素。如果该变量的(work)src与我桌面上的图像的src匹配(灯泡打开时) )然后它变为关闭,否则它变为打开(好像它不打开,这意味着它关闭,所以我们改变它)

我接受了学校的帮助。我查到了类似的问题。我甚至把work.src改成bulb.src。还是找不到我的错误。请帮帮我,告诉我这是什么原因!!!请不要用jQuery,因为我还不知道

if (work.src.match(C:\Users\hp\Desktop\on)) {
这应该与字符串匹配吗

if (work.src.match('C:\Users\hp\Desktop\on')) {

您正在使用
match
函数,该函数需要一个
RegExp
作为参数。实际上,我认为在您的情况下,您可以比较
字符串

if (work.src === 'C:\Users\hp\Desktop\on') {
document.getElementById('bull')不是图像。在您的代码中,您正在更改
的“src”

将id更改为图像,如:

<a href="#" id="blabla" onclick="changesrc(); return false;">click</a> 
<div style="text-align:center;"">
<img id="bulb" src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/se/se-logo-med.png?v=d9b0b6647f17" style="width:180px;height:270px;position:relative;top:25px;border:2px solid black;">
</div>
更新


“return”已更正:
onclick=“changesrc();return false;“

“我是java初学者,请帮助我。”-有两件事,第一件事:这不是java,这是JavaScript(无论如何,它们不是同一件事,甚至是密切相关的);第二:作为一个初学者并不能减轻你的问题的要求,因为这个网站明确为“非初学者”而设。到目前为止,每个人都忽略了在回答中避开反斜杠:
'C:\\Users\\hp\\Desktop\\on'
是你想要的。@WilliamKunkel好的观点:D!
<a href="#" id="blabla" onclick="changesrc(); return false;">click</a> 
<div style="text-align:center;"">
<img id="bulb" src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/se/se-logo-med.png?v=d9b0b6647f17" style="width:180px;height:270px;position:relative;top:25px;border:2px solid black;">
</div>
function changesrc(){
    var work = document.getElementById('bulb');

    if(work.src.indexOf('se/se-')){
       work.src = 'http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo-med.png?v=6f86a5fa447f';
    }
    else{
        work.src = 'http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/se/se-logo-med.png?v=d9b0b6647f17';
    }
}