Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 显示一个div并隐藏打开的任何其他div_Jquery_Jsp - Fatal编程技术网

Jquery 显示一个div并隐藏打开的任何其他div

Jquery 显示一个div并隐藏打开的任何其他div,jquery,jsp,Jquery,Jsp,我试图一次显示一个div,所以当按下按钮时,它会显示该div并隐藏其他div(如果其中任何一个可见)。我试过几个例子,但都失败了。下面的代码一次单击就隐藏了所有显示的div <% for(var i = 0; i < 3; i++){ %> <button class="but<%= i %>" >Show Skeetchpad</button> <div id="p<%=i%>" >&

我试图一次显示一个div,所以当按下按钮时,它会显示该div并隐藏其他div(如果其中任何一个可见)。我试过几个例子,但都失败了。下面的代码一次单击就隐藏了所有显示的div

<% for(var i = 0; i < 3; i++){ %>
        <button class="but<%= i %>" >Show Skeetchpad</button>
        <div id="p<%=i%>" ></div>

                    <script>
                        $(document).ready(function(){
                         $("#p<%= i %>").hide();
                            $(".but<%= i %>").click(function(){

                                if ($.trim($(this).text()) === 'Show Sketchpad') {
                                    $(this).parent().siblings().hide();
                                    $("#p<%= i%>").load("sketch.jsp");
                                    $("#p<%= i %>").show("slow");                                       
                                    $(this).text('Hide Sketchpad').css("font-weight","bold");
                                } else {
                                    $(this).text('Show Sketchpad').css("font-weight","normal");
                                    location.reload(false);
                                    $("#p<%= i %>").hide("slow");
                                }
                                return false; 
                            }); 
                        });
                    </script>    
                    <hr>
                    <%
                        }
                    %>

显示Skeetchpad
$(文档).ready(函数(){
$(“#p”).hide();
$(“.but”)。单击(函数(){
if($.trim($(this).text())===='显示画板'){
$(this.parent().sides().hide();
$(“#p”).load(“sketch.jsp”);
$(#p)显示(“慢”);
$(this).text('Hide Sketchpad').css(“字体重量”、“粗体”);
}否则{
$(this).text('Show Sketchpad').css(“字体重量”,“正常”);
位置。重新加载(错误);
$(“p”)。隐藏(“慢”);
}
返回false;
}); 
});

$('button')。在('click',function()上{
var c='.+$(this.attr('class');
$('div').not(c.hide();
$(c.show();
})
div{
显示:无;
}

第一组
第二组
第三组
第一组
第二组

div3在按钮处理程序中添加以下代码段

<%-- Get the class name of the button that is clicked --%>
var buttonClass = $(this).attr('class');
<%-- Get the div ID corresponding to the button clicked.
E.g. if the button clicked is but3, the corresponding div that needs to be shown
is p3. The remaining ones should be hidden
 --%>
var divId = "p" + buttonClass[buttonClass.length - 1];
<%-- Hide all the divs the IDs of which start with a 'p' --%>
$("div[id^=p]").not(divId).hide();

var buttonClass=$(this.attr('class');
var divId=“p”+buttonClass[buttonClass.length-1];
$(“div[id^=p]”)而不是(divId.hide();

您需要为我们创建一个简单、实用的演示。它不一定是你的具体项目,只是一些能让你明白重点的东西。是不是因为按钮文本是“显示草图板”,所以如果($.trim($(this.text())===='Show Sketchpad'){
,则该值将始终计算为false-