如何为此 jquery 代码使用函数参数,或者是否有更好的解决方案
How do I use a function argument for this jquery code, or is there a better solution?
我有大约 50 个 p 标签,旁边又是 50 个div。 单击每个 p 标签时,应显示其div,其余部分隐藏。我如何实现这一点。我可以使用如下所示的内容:
$(function() {
$('.p1').click(function(){
$('.div1').show();
$('.div2','.div3','.div4','.div5','.div6',.........,'.div50').hide()
})
$('.p2').click(function(){
$('.div2').show();
$('.div1','.div3','.div4','.div5','.div6',.........,'.div50').hide()
})
//////////////
//////
})
但如您所见,这不是一个有效的解决方案。我也不确定如何在这里利用 jquery each
,或者如何使用数组完成此实现。有人可以指出我正确的方向吗?我认为我们应该使用一个函数并传递那个 no。作为参数,但我不知道如何在 jquery 中使用自定义函数。
更新:
这就是我所做的
$(function() {
$('.p1').click(function() {
$('.div').hide();
$('.d1').show();
})
})
我已经将类div 添加到我所有的 50 个div 中,并且在单击 p1 时显示 d1。现在如何为每个实例替换 1 直到 50。
我会为所有div
和p
提供一个公共类,以便处理程序和hide
的绑定可以很简单。对于div,我会将数据标签关联到每个p
,以将每个p
标签链接到div
<p class="p1 pclass" data-showdiv="div1">
...
</p>
<p class="p2 pclass" data-showdiv="div2">
..
<div class="mydiv div1" ..>
..
</div>
<div class="mydiv div2" ..>
..
</div>
剧本将是,
$(function() {
$('.pclass').click(function(){
$('.mydiv').hide();
$('.' + $(this).data('showdiv')).show();
});
});
正如杰森所说,
使用这个
$('p').click(function() {
$('div').hide();
$(this).next('div').show();
});
如果div
位于每个段落旁边。
但是,如果p
和div
之间有一个元素,它将不起作用。
对于你的问题,你可以做,
$('p').click(function() {
$('div').hide();
var divClass = $(this).attr("class").replace('p','div');
$('.' + divClass).show();
});
只要你只有p1
,p2
....在帕拉格拉类;)
更新
看到这个小提琴
请注意,我们可以根据需要在<p>
和<div>
之间<br>
标签。
假设您的 HTML 结构是
<p>Some text</p>
<div>More text to hide and show</div>
<p>Some text</p>
<div>More text to hide and show</div>
<p>Some text</p>
<div>More text to hide and show</div>
....
在$(function(){});
方法中使用以下方法:
$('p').click(function() {
$('div').hide();
$(this).next('div').show();
});
var dvs = ['.div1','.div2','.div3','.div4','.div5','.div6',.........,'.div50'];
$('p').click(function() {
var index = parseInt(this.className.replace('p','')) - 1;
$(dvs[index]).show();
$(dvs.join(', ')).not(dvs[index]).hide();
});
jQuery click
事件将自动注册到与选择器匹配的所有元素上,因此您不必使用 each()
方法。我建议有两个CSS类来区分具有这种切换行为的元素和主要的元素(即在单击其父元素时应该显示(。
标记:
<body>
<p class="togglable">
<div class="primary">
This is the primary div that will be shown when our parent is clicked.
</div>
<div>Regular div child</div>
<p>Nested paragraph</p>
<ul>
<li>A list perhaps</li>
</ul>
</p>
<p class="togglable">
<div class="primary">
This is the primary div that will be shown when our parent is clicked.
</div>
<div>Regular div child</div>
<p>Nested paragraph</p>
<ul>
<li>A list perhaps</li>
</ul>
</p>
<p>This is a normal paragraph</p>
</body>
代码:
$(function () {
$('.togglable').click(function () {
// hide all our children
$(this).children().hide();
// now only show our primary chlid
// NOTE: we pass 'this' as the second argument
// so that the selector will only apply to the
// children of the element that was clicked
// (i.e. we are providing a custom context for the selector).
$('.primary', this).show();
// You could even use the position of the child as well:
// $(this).children().first().show();
// This will show the first child element.
});
});
在此示例中,具有类 togglable
的所有元素在单击时将显示其主要子元素,并隐藏所有其他子元素。
相关文章:
- 使用$scope的最佳实践$apply();或者甚至是使用Angular Component的非Angular内容中的$
- 如何为javascript创建自定义标签?或者这是我不知道的其他事情吗;我不知道
- 检查'这'是$(这个)或者只是普通的旧'这'
- 我需要帮助理解javascript||.这是一个逻辑'或者'是否
- 同位素分页布局?或者至少是增量加载
- 未能构造'Blob':提供的第一个参数为null,或者是无效的Array对象
- 闭包中的本地变量从外部更改.这是一个javascript错误,或者我做错了什么
- 创建大量的js文件或者合并为一个文件是一种好的做法吗
- 模块是打字的好主意吗?或者它们只是增加了复杂性
- 一个简单的JS-PHP验证(或者我是这么认为的)
- 如果,或者,虽然不是..需要 JavaScript 帮助
- JSON 电子表格返回 null,或者我是个白痴
- 输入的值不是由 JavaScript 设置的(或者它是吗?)
- 对象的对象?或者可能是嵌套对象?在 JavaScript 中
- 角度隔离范围值可以通过“;真“;或者是空的
- 该节点/快递路由如何完成更新?或者是这样
- 获得网站词频计数的最佳方法?或者是网站的一部分
- 动态需要React/Webpack的图像,或者是许多需要的成本(img)
- GraphQL错误字段必须是一个以字段名作为键的对象,或者是一个返回这样一个对象的函数
- 恶意js ?或者是什么