2015-10-02 79 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); 

});