简单的可重用JavaScript toggle()脚本
Simple reusable JavaScript toggle() script?
我正在学习JavaScript,不知道如何处理一个简单的可重用代码片段。
我需要的是一段代码,它将隐藏()#body01、#body02、#bodi03、04,05等(所有这些)。然后,当我单击title01时,它明白我想切换()body01。如果我单击title02,它将切换()body02,依此类推。
<a id="title01">Title 1</a>
<div id="body01">Body content for Title 1</div>
<a id="title02">Title 2</a>
<div id="body02">Body content for Title 2</div>
<a id="title03">Title 3</a>
<div id="body03">Body content for Title 3</div>
很抱歉,如果有人问我这个问题,我自己也找不到,也弄不清楚。
谢谢!
如果你使用jQuery(如果没有,你应该使用),它就这么简单:
$('[id^=title]').click(function(){
var tmp = $(this).attr('id').split("title");
$('#body'+tmp[1]).toggle();
});
您可以使用toggle
方法对jQuery执行以下操作:
$(function(){
$("[id^=title]").click(function(){
$(this).next().toggle();
return false; // prevent moving down or going to link
});
});
新的JS开发人员经常直接进入jQuery,而没有首先学习JavaScript作为一种语言。你肯定应该至少学习一些简单的JavaScript,这样你才能更好地理解和使用jQuery强大的功能。切换的最佳方式是设置和删除类,而不是在元素上设置样式属性。话虽如此,你可以做这样的事。
.hidden{display:none;}
<div>
<a id="title01" class="toggler">Title 1</a>
<div class="body" id="body01">Body content for Title 1</div>
<a id="title02" class="toggler">Title 2</a>
<div class="body" id="body02">Body content for Title 2</div>
<a id="title03" class="toggler">Title 3</a>
<div class="body" id="body03">Body content for Title 3</div>
</div>
<script>
// set your initial variables
// ideally you will put these in a function which is not
// exposed to the global object
var togglers = document.getElementsByTagName('a'),
divs = document.getElementsByTagName('div'),
i, j;
// here you loop through your a elements and if they have
// a class of toggler you assign the onclick event to a toggle function
for ( i = 0; i < togglers.length; i += 1) {
if (togglers[i].className == 'toggler') {
togglers[i].onclick = toggle;
}
}
function toggle() {
// here you will cache the variable toToggle
// which is the div you want to toggle
var toToggle;
// loop through all divs and if they have a class of body
// you hide it
for (j = 0; j < divs.length; j += 1) {
if (divs[j].className == 'body') {
divs[j].className += ' hidden';
// this is tricky for a beginner. nodeType 1 is an element node
// nextSibling will get the nextSibling but if there is white space
// in your document it will return a text node when you have to toggle
// an element node. this just ensures that it will keep going until it
// finds and element node
if (this.nextSibling.nodeType !== 1) {
toToggle = this.nextSibling.nextSibling;
}
// then you toggle it by removing the hidden class
toToggle.className = 'body';
}
}
}
</script>
</body>
这里有几个链接指向nodeType和下一个同级节点。https://developer.mozilla.org/en/nodeType
https://developer.mozilla.org/En/DOM/Node.nextSibling
将类添加到DOM元素中,如下所示:
<a class="title" id="title01" href="">Title 1</a>
<div class="body" id="body01">Body content for Title 1</div>
<a class="title" id="title02" href="">Title 2</a>
<div class="body" id="body02">Body content for Title 2</div>
<a class="title" id="title03" href="">Title 3</a>
<div class="body" id="body03">Body content for Title 3</div>
然后你可以添加这样的切换功能:
$('.title').click(function(e){
e.preventDefault();
$(this).next('.body').toggle();
});
下面是类似手风琴的jQuery功能
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 借助asp.net验证或java脚本对多个文本进行验证
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- Java脚本时间添加
- 不显示带有本地json文件数据的谷歌地图脚本
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- jQuery/Javascript.toggleClass/.classList.toggle的特殊行为
- 从远程脚本获取用户IP
- 如何根据时间运行不同的脚本
- 如何将字符串值从php页面发送到java脚本页面
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 当脚本由system.js加载时,如何要求('electron')
- HTML标记在脚本标记中工作
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 显示时间的脚本
- ng应用程序使脚本无限运行
- 如何使用JQuery toggle和AJAX调用PHP脚本
- 简单的可重用JavaScript toggle()脚本