下面的鏈接是一個項目,我目前工作: http://feathertest.me.pn/p-indoor.php#prod-stop如何使用單擊處理程序而不是多個任務?
在上面的鏈接提供的,我有合適的可用的可點擊的顏色列表。基於用戶點擊的顏色,左側的圖像會相應地改變。
以下是代碼片段我用的是特殊的功能:
$(function() {
$('#showdiv1').click(function() {
$('div[id^=div]').hide();
$('#div1').show();
});
$('#showdiv2').click(function() {
$('div[id^=div]').hide();
$('#div2').show();
});
$('#showdiv3').click(function() {
$('div[id^=div]').hide();
$('#div3').show();
});
$('#showdiv4').click(function() {
$('div[id^=div]').hide();
$('#div4').show();
});
})
我的HTML代碼片段:
<!-- mobile version start -->
<div class="row hidden-lg hidden-md hidden-sm">
<div class="col-md-5 col-sm-5 col-xs-8 col-xs-push-2 col-sm-push-1 col-md-push-1">
<p class="prod-code">FB - 329</p>
<p class="prod-title">Monara</p>
<p class="prod-desc">The best in class from our range of Indoor Brooms. This is the only broom in the market that has five rows of syntetic bristles. A "Heavy Duty" broom suitable for high dust areas.</p>
<div class="prod-det">
<ul>
<li>Sweeps away more dirt at once while leaving no traces behind. </li>
<li>Split bristle edges for collecting fine dust.</li>
<li>Head enclosure constructed using the highest graded plastic material.</li>
<li>Uniform Italian handle thread.</li>
<li>Angled edges to reach corners.</li>
<li>150g of high quality synthetic bristles with excellent bend recovery for prolonged usage.</li>
</ul>
</div>
<br>
<div class="color-change"><b>Colours Available:</b></div>
<div class="color-box">
<a id="tmonara-showdiv1" class="cb-pp" href="p-indoor.php#prod-stop" title="Purple"></a>
<a id="tmonara-showdiv2" class="cb-b" href="p-indoor.php#prod-stop" title="Black"></a>
<a id="tmonara-showdiv3" class="cb-o" href="p-indoor.php#prod-stop" title="Orange"></a>
<a id="tmonara-showdiv4" class="cb-p" href="p-indoor.php#prod-stop" title="Pink"></a>
</div>
<br><br>
<p class="color-change"><i>(click to change colors of product)</i></p>
</div> <!-- prod details end -->
</div>
<!-- mobile version end -->
<div id="feather-prods" class="row">
<div id="div1">
<div id="monara-purple" class="col-md-5 col-sm-6 col-xs-8 col-xs-push-2 col-sm-push-1 col-md-push-1">
<div class="prod-img"><img class="bigImg" src="images/broom/monara-pp-1.jpg" alt="feather, Purple monara broom"></div>
<div class="thumbnailImg">
<div class="thumb"><img src="images/broom/monara-pp-1.jpg" alt="feather, Purple monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb"><img src="images/broom/monara-pp-2.jpg" alt="feather, Purple monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb"><img src="images/broom/monara-pp-3.jpg" alt="feather, Purple monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb"><img src="images/broom/monara-pp-4.jpg" alt="feather, Purple monara broom" border="0" width="100%" class="thumbImg" /></div>
</div>
</div>
</div> <!-- prod 1 end -->
<div id="div2" style="display:none;" >
<div id="monara-green" class="col-md-5 col-sm-6 col-xs-8 col-xs-push-2 col-sm-push-1 col-md-push-1">
<div class="prod-img"><img class="bigImg2" src="images/broom/monara-g-1.jpg" alt="feather, green monara broom"></div>
<div class="thumbnailImg">
<div class="thumb2"><img src="images/broom/monara-g-1.jpg" alt="feather, green monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb2"><img src="images/broom/monara-g-2.jpg" alt="feather, green monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb2"><img src="images/broom/monara-g-3.jpg" alt="feather, green monara broom" border="0" width="100%" class="thumbImg" /></div>
<div class="thumb2"><img src="images/broom/monara-g-4.jpg" alt="feather, green monara broom" border="0" width="100%" class="thumbImg" /></div>
</div>
</div>
</div> <!-- prod 2 end -->
<div class="col-md-5 col-sm-5 col-xs-8 col-xs-push-2 col-sm-push-1 col-md-push-1 hidden-xs">
<p class="prod-code">FB - 329</p>
<p class="prod-title">Monara</p>
<p class="prod-desc">The best in class from our range of Indoor Brooms. This is the only broom in the market that has five rows of syntetic bristles. A "Heavy Duty" broom suitable for high dust areas.</p>
<br>
<div class="color-change"><b>Colours Available:</b></div>
<div class="color-box">
<a id="monara-showdiv1" class="cb-pp" href="p-indoor.php#prod-stop" title="Purple"></a>
<a id="monara-showdiv2" class="cb-g" href="p-indoor.php#prod-stop" title="Green"></a>
<a id="monara-showdiv3" class="cb-b" href="p-indoor.php#prod-stop" title="Black"></a>
<a id="monara-showdiv4" class="cb-o" href="p-indoor.php#prod-stop" title="Orange"></a>
<a id="monara-showdiv4" class="cb-p" href="p-indoor.php#prod-stop" title="Pink"></a>
</div>
<br><br>
<p class="color-change"><i>(click to change colors of product)</i></p>
<div class="prod-det">
<ul>
<li>Sweeps away more dirt at once while leaving no traces behind. </li>
<li>Split bristle edges for collecting fine dust.</li>
<li>Head enclosure constructed using the highest graded plastic material.</li>
<li>Uniform Italian handle thread.</li>
<li>Angled edges to reach corners.</li>
<li>150g of high quality synthetic bristles with excellent bend recovery for prolonged usage.</li>
</ul>
</div>
</div> <!-- prod details end -->
</div>
我有2個問題:
有沒有一種縮短jQuery代碼的方法?這意味着,我可以使用一個點擊處理程序來實現相同的任務,而不是使用多個點擊處理程序?當談到jQuery時,我仍然是一個noob。
如果您發現我有移動視圖的註釋部分(我必須爲移動視圖創建單獨的文本塊,因爲我需要對移動視口的設計進行一些更改)。我注意到,一旦我創建了移動視圖部分,上面的jQuery就停止了桌面視圖的工作,並且只能在移動視圖中工作,因此我必須爲移動視圖重新創建具有不同ID值的同一塊jQuery代碼。
如何讓相同的jQuery代碼(如上所述)適用於移動視圖和桌面視圖?
投了一個比我更完整的答案。 –
謝謝@VanquishedWombat :) –
@JuliaNething一切工作!非常感謝!!!! :D –