下面的链接是一个项目,我目前工作: 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 –