使用单选按钮而不是列表导航的 jQuery 选项卡
我正在按照教程创建一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是解决方案:
Here is the solution:
在您有此行的地方
您可能会将其更改为
然后更改,例如
假设
单选按钮的值是“gallery”
所有这一切的要点是您从单选按钮上选择的属性中选择什么相应的div的id将是。您可以根据需要调整特定的字符串和属性。
Where you have this line
You would probably change it to
and then change, for example
to
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.
我只是遵循了这个教程,但无法让它工作几个小时,所以我为其他可能也看到这个问题的人做了一个解决方法。
诀窍在于
它返回并选择第一个元素,该元素是 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.
the trick lies herein
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 )
http://www.aspmvcnet.com/genel/jquery-radio-button -tab.aspx 如果你看这篇文章,你可以到达这个主题的详细信息
http://www.aspmvcnet.com/genel/jquery-radio-button-tab.aspx if you look this article, you can reach details of this subject
在谷歌搜索时,找到这个有趣的话题。经过尝试,我有一个解决方案。使用 click() 来激活无线电速度很慢。有一个更好的方法。使用“val()”来获取radio的值,而不是“attr('value')”。完整代码如下,已测试。
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.