gpt4 book ai didi

javascript - jQuery 在 Wordpress 中点击显示/隐藏

转载 作者:行者123 更新时间:2023-11-28 03:21:21 25 4
gpt4 key购买 nike

我有三个按钮,它们会在您悬停时改变颜色( Sprite ),我想使用这些按钮,以便在单击它们时显示不同的内容。

我浏览了多个教程/看板,但似乎没有任何效果。

按钮显示如下:

<div id="buttons">
<a href="#" id="1"class="des"></a>
<a href="#" id="2"class="brnd"></a>
<a href="#" id="3"class="strt"></a>
</div>

放置内容的 div(我最后一次尝试)如下:

<div id="pages">
<div id="div1"><img src="..."></></div>
<div id="div2"><img src="..."></></div>
<div id="div3"><img src="..."></></div>

jQuery----

<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

</head>

<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var id = $(this).attribute("data-id"); // Using a custom attribute.
$("#pages div").hide(); // gather all the div tags under the element with the id pages and hide them.
$(".div" + id).show(); // Show the div with the class of .divX where X is the number stored in the data-id of the object that was clicked.
});
});
</script>​​​​

我的 anchor 似乎是错误的。有了这个设置,它就到了页面的顶部。使用 anchor #1、#2 或 #3 时,它会转到 div 位置,但不会隐藏或显示内容。令人沮丧。

Sprite 工作正常。现在我想弄清楚如何使它们可点击,以便在单击每个按钮时显示不同的内容(3 个 div - 在父 div 下?)。如果有人确切地知道如何做到这一点,我将非常感激。

内容主要是图片,我在前端编辑器中使用了 Jupiter 主题,所以我不知道它是否与此有关。但后端似乎没有任何问题。

另外,如果你能给我指一个教程,教我如何制作它,以便它们在单击时动画进出,那将是合法的。再次感谢。

最佳答案

查看 my fiddle .

您发布的代码有两个主要问题,混淆了 jQuery 的数据属性和 Javascript id,以及混淆了类 (.) 和 id (#) 的 CSS 选择器。这是更正后的 html 和 javascript:

HTML

<div id="buttons">
<a href="#" data-id="1" class="des">Des</a>
<a href="#" data-id="2" class="brnd">Brnd</a>
<a href="#" data-id="3" class="strt">Strt</a>
</div>

<div id="pages">
<div id="div1"><p>This is 1</p></div>
<div id="div2"><p>This is 2</p></div>
<div id="div3"><p>This is 3</p></div>
</div>

Javascript

$("#pages div").hide();

$("a").click(function() {
var id = $(this).data("id");
$("#pages div").hide();
$("#div" + id).show();
});

关于javascript - jQuery 在 Wordpress 中点击显示/隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com