使用单选按钮而不是列表导航的 jQuery 选项卡

发布于 2024-08-13 08:53:03 字数 2110 浏览 5 评论 0原文

我正在按照教程创建一个简单的 jquery 选项卡显示/隐藏内容。

想知道是否有办法重新设计它以使用单选按钮列表而不是列表?

教程在这里: http://www.sohtanaka.com/web-design /simple-tabs-w-css-jquery/

我的 js:

    $(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content

//On Click Event
$("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active").children("input[@type=radio]").click(); //Add "active" class to selected tab
    $(".tab_content").hide(); //Hide all tab content
    var activeTab = "#" + $(this).children("input").attr("value"); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
});

我的 HTML:

    <ul class="tabs">
    <li><input type="radio" name="card" id="one" value="gallery" /> <label for="one">gallery</label></li>
    <li><input type="radio" name="card" id="two" value="submit" /> <label for="two">submit</label></li>
    <li><input type="radio" name="card" id="three" value="resources" /> <label for="three">resources</label></li>
    <li><input type="radio" name="card" id="four" value="contact" /> <label for="four">contact</label></li>
</ul>
<div class="tab_container">
    <div id="gallery" class="tab_content">
        <h2>Gallery</h2>
    </div>
    <div id="submit" class="tab_content">
        <h2>Submit</h2>
    </div>
    <div id="resources" class="tab_content">
        <h2>Resources</h2>
    </div>
    <div id="contact" class="tab_content">
        <h2>Contact</h2>
    </div>
</div>

我能够主动选择列表中的单选按钮,但无法激活实际的 div。

I'm following a tutorial to create a simple jquery tabs show/hide content.

Wondering if there's a way to re-engineer it to use a list of radio buttons instead of a list?

Tutorial here:
http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/

My js:

    $(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content

//On Click Event
$("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active").children("input[@type=radio]").click(); //Add "active" class to selected tab
    $(".tab_content").hide(); //Hide all tab content
    var activeTab = "#" + $(this).children("input").attr("value"); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
});

My HTML:

    <ul class="tabs">
    <li><input type="radio" name="card" id="one" value="gallery" /> <label for="one">gallery</label></li>
    <li><input type="radio" name="card" id="two" value="submit" /> <label for="two">submit</label></li>
    <li><input type="radio" name="card" id="three" value="resources" /> <label for="three">resources</label></li>
    <li><input type="radio" name="card" id="four" value="contact" /> <label for="four">contact</label></li>
</ul>
<div class="tab_container">
    <div id="gallery" class="tab_content">
        <h2>Gallery</h2>
    </div>
    <div id="submit" class="tab_content">
        <h2>Submit</h2>
    </div>
    <div id="resources" class="tab_content">
        <h2>Resources</h2>
    </div>
    <div id="contact" class="tab_content">
        <h2>Contact</h2>
    </div>
</div>

I'm able to actively select the radio button within the list, but not activate the actual div.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情栀口红 2024-08-20 08:53:03

这是解决方案:

<div id="tabs">

<ul class="tabs">

     <li id="tab-1"><label for="tab1">For Sale<input name="tab" type="radio" value="forsale" /></label></li>
     <li id="tab-2"><label for="tab2">Wanted<input name="tab" type="radio" value="wanted" /></label></li>
</ul>

 <div class="tab_container">     

      <div id="forsale" class="tab_content">for sale</div>

      <div id="wanted" class="tab_content">wanted</div>

</div>

jQuery(document).ready(function($) {

                //Default Action
                $(".tab_content").hide(); //Hide all content
                $("ul.tabs li:first").addClass("active").show().find("label input:radio").attr("checked",""); //Activate first tab
                $(".tab_content:first").show(); //Show first tab content

                //On Click Event
                $("ul.tabs li").click(function() {

                    //$("ul.tabs li").removeClass("active"); //Remove any "active" class
                    //$(this).addClass("active"); //Add "active" class to selected tab
                    //$(".tab_content").hide(); //Hide all tab content
                    //var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
                    //$(activeTab).fadeIn(); //Fade in the active content
                    //return false;
                    $("ul.tabs li").removeClass("active"); //Remove any "active" class
                    $("ul.tabs li").find("label input:radio").attr("checked","");
                    $(this).addClass("active").find("label input:radio").attr("checked","checked");
                    $(".tab_content").hide(); //Hide all tab content
                    var activeTab = $(this).find("label input:radio").val(); //Find the href attribute value to identify the active tab + content
                    $('#' + activeTab).fadeIn(); //Fade in the active ID content
                    return false;

                });

            });

Here is the solution:

<div id="tabs">

<ul class="tabs">

     <li id="tab-1"><label for="tab1">For Sale<input name="tab" type="radio" value="forsale" /></label></li>
     <li id="tab-2"><label for="tab2">Wanted<input name="tab" type="radio" value="wanted" /></label></li>
</ul>

 <div class="tab_container">     

      <div id="forsale" class="tab_content">for sale</div>

      <div id="wanted" class="tab_content">wanted</div>

</div>

jQuery(document).ready(function($) {

                //Default Action
                $(".tab_content").hide(); //Hide all content
                $("ul.tabs li:first").addClass("active").show().find("label input:radio").attr("checked",""); //Activate first tab
                $(".tab_content:first").show(); //Show first tab content

                //On Click Event
                $("ul.tabs li").click(function() {

                    //$("ul.tabs li").removeClass("active"); //Remove any "active" class
                    //$(this).addClass("active"); //Add "active" class to selected tab
                    //$(".tab_content").hide(); //Hide all tab content
                    //var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
                    //$(activeTab).fadeIn(); //Fade in the active content
                    //return false;
                    $("ul.tabs li").removeClass("active"); //Remove any "active" class
                    $("ul.tabs li").find("label input:radio").attr("checked","");
                    $(this).addClass("active").find("label input:radio").attr("checked","checked");
                    $(".tab_content").hide(); //Hide all tab content
                    var activeTab = $(this).find("label input:radio").val(); //Find the href attribute value to identify the active tab + content
                    $('#' + activeTab).fadeIn(); //Fade in the active ID content
                    return false;

                });

            });
極樂鬼 2024-08-20 08:53:03

在您有此行的地方

var activeTab = $(this).find("value").attr("href");

您可能会将其更改为

var activeTab = "#" + $(this).attr("value");

然后更改,例如

<div id="tab2" class="tab_content">

假设

<div id="gallery" class="tab_content">

单选按钮的值是“gallery”

所有这一切的要点是您从单选按钮上选择的属性中选择什么相应的div的id将是。您可以根据需要调整特定的字符串和属性。

Where you have this line

var activeTab = $(this).find("value").attr("href");

You would probably change it to

var activeTab = "#" + $(this).attr("value");

and then change, for example

<div id="tab2" class="tab_content">

to

<div id="gallery" class="tab_content">

Assuming the value of the radio button is "gallery"

The point of all this being that you are selecting from an attribute on the radio button that is selected what the id of the corresponding div would be. You can adjust the particular string and attributes as needed.

奶气 2024-08-20 08:53:03

我只是遵循了这个教程,但无法让它工作几个小时,所以我为其他可能也看到这个问题的人做了一个解决方法。

<li><a href="#fragment-7"><span><input type="radio" checked="yes" name="b" id="5">
                <img onclick="this.parentNode.childNodes[0].checked=true" 
                src="http://www.jgiesen.de/javascript/JavaScript/JSBeispiele/gifs/upArrow.gif" style="margin-left:-20px"></span></a></li>

诀窍在于

onclick="this.parentNode.childNodes[0].checked=true"

它返回并选择第一个元素,该元素是 jquery 样式的,对于任何选项卡都是正确的。

通过设置 margin-left:-20px 你可以得到实际单选按钮后面的图片。因此,让你的背景图像要么完全透明,要么在左侧保留一些透明的 25 像素。

然后只需放置正确大小的图像或使用 img 属性宽度/高度

此解决方法不需要弄乱 jquery 代码,它很简单

(另一种选择:
使用这个“旧”jquery 选项卡 - 将选择收音机并且选项卡也会:
http://stilbuero.de/jquery/tabs/#fragment-7 )

i just followed this tut and could not get it working for hours so i made a workaround for others that might see this too.

<li><a href="#fragment-7"><span><input type="radio" checked="yes" name="b" id="5">
                <img onclick="this.parentNode.childNodes[0].checked=true" 
                src="http://www.jgiesen.de/javascript/JavaScript/JSBeispiele/gifs/upArrow.gif" style="margin-left:-20px"></span></a></li>

the trick lies herein

onclick="this.parentNode.childNodes[0].checked=true"

it goes back and than selects the first element which is in jquery style for any tab correct.

with setting margin-left:-20px you get the pic behind the actual radio button. so make your background image either complete transparent or save some 25 px on the left which are transparent.

then just place a image with the correct size or using the img attributes width/height

this workarround does not require to mess with jquery-code, its plain simple

( Another alternative :
use this "older" jquery tabs - the radio will be selected and the tab also:
http://stilbuero.de/jquery/tabs/#fragment-7 )

想念有你 2024-08-20 08:53:03
$("input[name='card']").change(function () {
    var deger = $(this).val();

    if (deger == "gallery") {
        $("#gallery").show();
        $("#submit").hide();
        $("#resources").hide();
        $("#contact").hide();
    }
    else if (deger == "submit") {
        $("#gallery").hide();
        $("#submit").show();
        $("#resources").hide();
        $("#contact").hide();
    }
    else if (deger == "resources") {
        $("#gallery").hide();
        $("#submit").hide();
        $("#resources").show();
        $("#contact").hide();
    }
    else{
        $("#gallery").hide();
        $("#submit").hide();
        $("#resources").hide();
        $("#contact").show();
    }
});

http://www.aspmvcnet.com/genel/jquery-radio-button -tab.aspx 如果你看这篇文章,你可以到达这个主题的详细信息

$("input[name='card']").change(function () {
    var deger = $(this).val();

    if (deger == "gallery") {
        $("#gallery").show();
        $("#submit").hide();
        $("#resources").hide();
        $("#contact").hide();
    }
    else if (deger == "submit") {
        $("#gallery").hide();
        $("#submit").show();
        $("#resources").hide();
        $("#contact").hide();
    }
    else if (deger == "resources") {
        $("#gallery").hide();
        $("#submit").hide();
        $("#resources").show();
        $("#contact").hide();
    }
    else{
        $("#gallery").hide();
        $("#submit").hide();
        $("#resources").hide();
        $("#contact").show();
    }
});

http://www.aspmvcnet.com/genel/jquery-radio-button-tab.aspx if you look this article, you can reach details of this subject

九命猫 2024-08-20 08:53:03

在谷歌搜索时,找到这个有趣的话题。经过尝试,我有一个解决方案。使用 click() 来激活无线电速度很慢。有一个更好的方法。使用“val()”来获取radio的值,而不是“attr('value')”。完整代码如下,已测试。

$(document).ready(function() {

//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content

//On Click Event
$("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active").children("input[@type=radio]").attr("checked","checked");
    $(".tab_content").hide(); //Hide all tab content
    var activeTab = "#" + $(this).children("input").val(); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
});

});

While googling, find this interesting topic. After try, I have a solution. Use click() to active the radio is slow. There is a better way.Use "val()" to get value of radio, instead of "attr('value')". Full code below, tested.

$(document).ready(function() {

//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content

//On Click Event
$("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active").children("input[@type=radio]").attr("checked","checked");
    $(".tab_content").hide(); //Hide all tab content
    var activeTab = "#" + $(this).children("input").val(); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
});

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文