2015-10-02 81 views
0

我是新来的JS和我想弄清楚如何做一个简单的JS脚本,当用户触摸/点击一个div另一个div打开。提前致谢。如果有人能帮助我,我会非常感激。JS当点击一个div另一个div是开放的

+1

你用简单的JS或一些框架?你现在有任何代码吗?一些jsfiddle你的工作将是有帮助的 – zucker

+0

你是什么意思的div开放? – Bit68

+0

是的,你需要先分享你的代码片段。没有代码,我想你想以编程方式生成一个元素的点击。这是jQuery版本:http://stackoverflow.com/questions/2847185/how-to-programmatically-trigger-the-click-on-a-link-using-jquery。这是非jQuery的版本:http://stackoverflow.com/questions/906486/how-can-i-programmatically-invoke-an-onclick-event-from-a-anchor-tag-while-kee – Hammer

回答

0

您可以使用.target伪类的。对于这个定义下一个CSS规则:

HTML:

<div class="product"> 
    <img src="http://placehold.it/100x100"/> 
    <a href="#shoes">Show Shoes</a> 
</div> 

<div class="product-highlight" id="shoes"> 
    <p>These are the shoes</p> 
</div> 

CSS:

#shoes { 
    display: none; /* hide by default */ 
} 
#shoes:target, /* and show either if class show is present (on click) */ 
#shoes.show { /* or location hash matches id "shoes" */ 
    display: block; 
} 

和JS你想补充类show:

$(document).ready(function() { 

    $('.product-highlight').hide(); 

    $('a[href$=shoes').click(function() { 
    $('#shoes').addClass('show'); 
    }); 

}); 

当从索引页重定向你也需要设置一个哈希#shoes:

$(document).ready(function() { 

    $('a[href$=shoes]').click(function() { 

    window.location.href= 'http://sample.com/products.php/#shoes'; 

    }); 
}); 

Refer This page

$(document).ready(function(){ 
    $(".Test2").hide(); 
    $(".Test1").show(); 

    $('.Test1').click(function(){ 
     $(".Test2").slideToggle(); 
    }); 
}); 

WORKING FIDDLE

+0

我已经试图修改脚本,但我走到了死胡同。你可以看看我做了什么,并告诉我做错了什么。我会非常感激,我为失去你的时间而道歉。提前致谢。 JS Fiddle链接---> http://jsfiddle.net/fu2k5sch/2/ –

+0

http://jsfiddle.net/ivinraj/pgssep6c/ @DimchoStoimenov –

0

这可能会给你的想法你想要什么......

HTML

<div style="background-color: red"> 
<a href="#" id="panel1">Panel1</a> 
<p id="pm1" style="display:none;">Message Hello From Panel1</p> 
</div> 

<div style="background-color: Blue"> 
<a href="#" id ="panel2">Panel2</a> 
<p id="pm2" style="display:none;">Message Hello From Panel2</p> 
</div> 

JQUERY

$('#panel1').click(function(){ 

$("#pm1").show(1000); 
$("#pm2").hide(1000); 

}); 

$('#panel2').click(function(){ 

$("#pm1").hide(1000); 
$("#pm2").show(1000); 

});