Jquery hide不能在.on()中工作
Jquery hide not working inside .on()
我正在加载div帮助页面到index.html内的div。加载在我的index.html页面上正常工作。问题是,一旦帮助页div加载到index.html的div下的helpSection全部出现,不消失,直到我能够单击其中一个div。我想要的是只有第一个helpDiv显示和所有其他隐藏,如果这是不可能的,只是所有div隐藏。我试过玩jquery代码,但一直无法成功地得到这个工作。
jsfiddle例子
任何帮助都将是非常感激的。
index . html
<body>
<div id="loadHelpHere"></div>
</body>
Html代码
<div id="helpPage">
<div id="helpMenu">
<h4>Help Menu</h4>
<ul id="menu">
<li class="current_page_item justClick"><a href="#" data-id="div1">Help Section 1</a>
</li>
<li class="justClick"><a href="#" data-id="div2">Help Section 2</a>
</li>
</ul>
</div>
<div id="helpSection">
<div class="helpDiv">
<header>Help Documentation</header>
<article>Works!</article>
</div>
<div class="helpDiv1">
<header>Help Documentation content 1</header>
<article>Help Section 1</article>
</div>
<div class="helpDiv2">
<header>Help Documentation content 2</header>
<article>Help Section 2</article>
</div>
</div>
jquery $(document).ready(function(){
$( "#loadHelpHere" ).load( 'help.html #helpPage' );
$(document).on('click', '.justClick', function (e) {
$('#helpSection div').not(".helpDiv").hide();
$('#helpSection div.helpDiv').html($('#helpSection div.helpDiv' + ($(this).index() + 1)).html());
});
});
我会在每个<div class="helpDiv">
上设置style="display: none"
,除了第一个"helpDiv"或放置一些css,如:
/* This will target classes containing the string "helpDiv" but not the class helpDiv */
[class*="helpDiv"]:not(.helpDiv) {
display: none;
}
然后像这样输入.justClick
:
$(document).on('click', '.justClick', function (e) {
// This will hide all divs that contain a class matching "helpDiv"
$('#helpSection [class*="helpDiv"]').hide();
// Grab the index of the current .justClick
var index = $(this).index() + 1;
// Show the corresponding "helpDiv"
$('#helpSection div.helpDiv' + index).show();
});
jsfiddle更新
所以要么添加一个CSS类来默认隐藏它们,要么在加载完成时将它们设置为隐藏。
#helpSection > div {
display : none;
}
您可以显示默认的内容并删除内容,或者只是切换元素的可见性,而不是替换默认的内容。
https://jsfiddle.net/pt74q4dk/1/set style="display:none;"在默认情况下您希望隐藏的div上,然后是$。当你需要的时候再给他们看。
$(document).ready(function(){
$( "#loadHelpHere" ).load( 'help.html #helpPage' );
$( "#loadHelpHere" ).find('#helpSection').hide();
});
还缺少div#loadHelpHere的结束标签
相关文章:
- JQuery.on(“keydown”)追加到页面时不工作
- jQuery 1.7.2:.on()在一个页面上工作;Don’别再干别的了
- 无法使以下ng on click outside指令在同一页面上的两个元素上工作
- jQuery on()-不;第一次点击时无法工作
- jQuery on() 方法只能通过 childSelector 间接工作
- JavaScript on Click 事件在 img 标签上无法正常工作
- jQuery on(change);不要第二次工作
- jQuery.on('点击',function()只在第二次点击时工作
- 上的jQuery.on('已结束')无法在移动设备上工作
- $('body').on('blur')在IE8中工作不好
- Couchbase on Nodejs - add () 方法的工作原理
- jQuery .on() 无法正常工作
- jquery 1.7 .on 无法正常工作
- model.on('change', function()) 无法正常工作
- Jquery (2.2.0) 单击侦听器在使用 .on() 时单击几下后停止工作,但 .click() 工作正常
- Jquery on event 不与选择器一起工作,因为它与 Jquery(document) 一起工作
- 无法让 port.emit 和 port.on 在 Firefox 附加组件中工作
- Jquery on单击“按类名不工作”
- JavaScript on change 函数工作得不好
- NodeJS'jquery'npm包不工作($.on())