Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 通过单击链接切换图像并禁用活动链接_Javascript_Jquery_Html - Fatal编程技术网

Javascript 通过单击链接切换图像并禁用活动链接

Javascript 通过单击链接切换图像并禁用活动链接,javascript,jquery,html,Javascript,Jquery,Html,我有一个网页,其中有许多div像下面的一个。它使用简单的javascript通过点击两个单独的链接来切换图像 HTML: <div id="test"> <img src="http://placehold.it/100x100" /> <div class="caption"> <h4> <a href="javascript:void(0)" onclick="toggleImg0('test')"

我有一个网页,其中有许多div像下面的一个。它使用简单的javascript通过点击两个单独的链接来切换图像

HTML:

<div id="test">
   <img src="http://placehold.it/100x100" />
   <div class="caption">
      <h4>
        <a href="javascript:void(0)" onclick="toggleImg0('test')">Image1</a> 
        <a href="javascript:void(0)" onclick="toggleImg1('test')">Image2</a>
        </h4>
   </div>
</div>
function toggleImg1(charName) {
   document.getElementById(charName).getElementsByTagName('img')[0].src = "http://placehold.it/150x150";
}

function toggleImg0(charName) {
   document.getElementById(charName).getElementsByTagName('img')[0].src = "http://placehold.it/100x100";
}
现在,我想禁用显示相应图像的链接(它应该像普通文本一样)。我怎样才能做到


查看完整的网页。

您使用的是JQuery,为什么不使用他的功能,为您的链接提供一个全局类
toggle\u img
,然后您可以使用定义您希望每个链接在单击时更改的
src
,最后定义
单击
事件并使用函数更改
src
属性,检查下面的示例

希望这有帮助


$('body')。在('click','上。切换\u img',函数(){
$('img').attr('src',$(this.data('src'));
})

JavaScript:

    <script type="text/javascript">

function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

  </script>       

功能切换\u可见性(id){
var e=document.getElementById(id);
如果(e.style.display=='block')
e、 style.display='none';
其他的
e、 style.display='block';
}
HTML

    <a href="#" class="bold" onclick="toggle_visibility('caption');">SHOW/HIDE Images</a>

    <div id="caption">Put your images in here and stuff</div>

把你的照片放在这里等等
加载页面时将隐藏div,单击“显示/隐藏图像”文本将切换可见性


我想这就是你想要的…:)

这里是另一个解决方案

  • 您可以创建一个onclick处理程序函数,而不是为每个锚标记创建两个函数

    范例

    <script>
    function toggleImg(charName,obj) {
        var image=document.getElementById('test').getElementsByTagName('img')[0];
        //store image1 and image2 href
        var img_source1= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT7j4zeqZB4theKrjb13e8XwhXXYpumwYEzTvhYclxrSrzS_5yJ";
        var img_source2="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA";
        if(image.src==img_source1){
            image.src=img_source2;
            //change anchor default styling for anchor press
            obj.style='pointer-events: none;color:grey';
           //enable anchor default styling for next anchor
            obj.nextElementSibling.style='pointer-events:auto'
        }
        else {
            image.src=img_source1
            obj.style=' pointer-events: none;color:grey';
            obj.previousElementSibling.style='pointer-events:auto'
        }
    }
    </script>
    
    
    功能切换(字符名,obj){
    var image=document.getElementById('test').getElementsByTagName('img')[0];
    //存储image1和image2 href
    变量img_source1=”https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT7j4zeqZB4theKrjb13e8XwhXXYpumwYEzTvhYclxrSrzS_5yJ";
    变量img_source2=”https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_biu_tArA”;
    if(image.src==img\u source1){
    image.src=img_source2;
    //更改锚定压力机的锚定默认样式
    object.style='pointer-events:无;颜色:灰色';
    //为下一个锚点启用锚点默认样式
    objectj.nextElementSibling.style='pointer-events:auto'
    }
    否则{
    image.src=img\u source1
    对象样式='指针事件:无;颜色:灰色';
    obj.previousElementSibling.style='pointer-events:auto'
    }
    }
    
  • 为每个锚元素添加一个id。下面是html的修改版本

    <div id="test">
       <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_BI__tArA" />
       <div class="caption">
          <h4>
            <a id='anchor1' href="javascript:void(0)" onclick="toggleImg('test',this)">Image1</a> 
            <a id='anchor2' href="javascript:void(0)" onclick="toggleImg('test',this)">Image2</a>
            </h4>
       </div>
    </div>
    
    
    
    这里是一个片段

    
    功能切换(字符名,obj){
    var image=document.getElementById('test').getElementsByTagName('img')[0];
    变量img_source1=”https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT7j4zeqZB4theKrjb13e8XwhXXYpumwYEzTvhYclxrSrzS_5yJ";
    变量img_source2=”https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQwoMrE0aNNuYH5AlJ3B-4PT5UMC2fdAwS19xHVsW-rf_biu_tArA”;
    if(image.src==img\u source1){
    image.src=img_source2;
    object.style='pointer-events:无;颜色:灰色';
    objectj.nextElementSibling.style='pointer-events:auto'
    }
    否则{
    image.src=img\u source1
    对象样式='指针事件:无;颜色:灰色';
    obj.previousElementSibling.style='pointer-events:auto'
    }
    }
    
    使用CSS
    背景图像
    属性。切换类,你真的在使用jQuery吗?您有jQuery标记,但没有使用jQuery函数访问DOM。我还需要一些脚本将活动图像的链接更改为简单文本。有办法吗。