需要 jQuery 代码重构帮助
对之前的更新,这就是我正在处理的内容:
<body>
<div class="header"> <img class="imgLogo" src="img/vegtablelogo.jpg"> </div>
<div id="thumbsContainer">
<div class="thumb" id="carrotThumb"> <img id="showCarrot" class="imgThumb" src="img/carot.jpg" onClick=setupVeg("showCarrot", "carrotBig") /> </div>
<div class="hidden" id="carrotBig"> <img class="imgBig" src="img/carot.jpg" /> </div>
<div class="thumb" id="brocThumb"> <img id="showBroc" class="imgThumb" src="img/brocoli.jpg" onClick=setupVeg("showBroc", "brocBig") /> </div>
<div class="hidden" id="brocBig"> <img class="imgBig" src="img/brocoli.jpg" /> </div>
</div>
<!-- end thumbs container -->
<script>
var active = "";
function setupVeg(thumbVeg, hiddenVeg) {
$("#" + thumbVeg).click(function() {
if (active != hiddenVeg) {
$("div.hidden").hide("fast");
$("#" + hiddenVeg).show("fast", function() {});
active = hiddenVeg;
}
else {
$("div.hidden").hide("fast");
active="";
}
});
}
$("div.hidden").click(function () {
$("div.hidden").hide("fast");
isAnyBig=false;
});
</script>
</body>
不幸的是,此代码无法正常工作。我借用了下面建议的解决方案。
如果它确实有效就好了! 任何建议,非常欢迎。
An update to before, here's what I'm dealing with:
<body>
<div class="header"> <img class="imgLogo" src="img/vegtablelogo.jpg"> </div>
<div id="thumbsContainer">
<div class="thumb" id="carrotThumb"> <img id="showCarrot" class="imgThumb" src="img/carot.jpg" onClick=setupVeg("showCarrot", "carrotBig") /> </div>
<div class="hidden" id="carrotBig"> <img class="imgBig" src="img/carot.jpg" /> </div>
<div class="thumb" id="brocThumb"> <img id="showBroc" class="imgThumb" src="img/brocoli.jpg" onClick=setupVeg("showBroc", "brocBig") /> </div>
<div class="hidden" id="brocBig"> <img class="imgBig" src="img/brocoli.jpg" /> </div>
</div>
<!-- end thumbs container -->
<script>
var active = "";
function setupVeg(thumbVeg, hiddenVeg) {
$("#" + thumbVeg).click(function() {
if (active != hiddenVeg) {
$("div.hidden").hide("fast");
$("#" + hiddenVeg).show("fast", function() {});
active = hiddenVeg;
}
else {
$("div.hidden").hide("fast");
active="";
}
});
}
$("div.hidden").click(function () {
$("div.hidden").hide("fast");
isAnyBig=false;
});
</script>
</body>
This code is not working unfortunately. I have borrowed from suggested solution below.
Would be nice if it did work!
Any suggestions, most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您实际上不需要任何标志或 if 条件。我认为你的逻辑是:
被点击。
所以你需要的是:
.toggle 将处理你的标志之一(isCarrotBig),如果 div.hidden 已经隐藏, .hide() 将不会执行任何操作,这样就可以处理你的 isAnyBig 标志。
现在..让我们也用broc 来实现这一点...
如果您有兴趣,您可以进一步重构它,这样您就不需要为每种蔬菜提供ID。不过我需要查看您的 HTML 标记。
I don't think you need any of the flags or the if conditions really. I think your logic is:
is clicked.
So all you need is:
.toggle will handle one of your flags (isCarrotBig) and .hide() won't do anything if div.hidden is already hidden, so that takes care of your isAnyBig flag.
Now.. let's make that work with broc as well...
If you're interested, you can refactor it FURTHER so you don't need to supply the IDs for each of the vegetables. I'll need to see your HTML markup though.
好的,我将发布一个新的答案来回应编辑。
值得注意的点:
您的代码已修改:
Ok I'll post a new answer in response to the edit.
Points worth noting:
Your code modified:
创建一个函数并重用它......类似:
然后
create a function and reuse it....something like:
and then
我个人建议创建一个简单的 jQuery 插件。像这样的东西:
然后你只需用这样的东西来调用它:
你的下一步应该是调查是否可以摆脱全局变量。
Personally I would suggest creating a simple jQuery plugin. Something like so:
Then you just call it with something like so:
Your next step should be to investigate whether you can get rid of the global variables or not.
好吧,我找到了一个简洁的解决方案,依赖于每个隐藏的 DIV 都是 .next() 的。如果不是的话,它将无法工作,但一般来说应该没问题。被黑了!
Ok I have found a neat(ish) sollution, dependent on each hidden DIV being the .next() one. If it isn't it won't work but should be fine generally though. Hacked!