Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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
Javascript 如何将jquery函数用于多个div_Javascript_Jquery - Fatal编程技术网

Javascript 如何将jquery函数用于多个div

Javascript 如何将jquery函数用于多个div,javascript,jquery,Javascript,Jquery,我想使用JavaScript/DOM属性为创建一个显示/隐藏函数 使用带有两个按钮的JavaScriptonclick(),show和hide,每个按钮都位于各自的中,其工作原理如下: 默认情况下,显示按钮隐藏,主和隐藏按钮显示。单击隐藏按钮隐藏主和“隐藏”按钮本身,显示显示按钮 单击show按钮显示主,“show”按钮显示hide按钮。我认为这可以通过使用类似document.getElementById(DIVid).style.visibility=“hidden”的方法实现 但是我该

我想使用JavaScript/DOM属性为
创建一个显示/隐藏函数

使用带有两个按钮的JavaScript
onclick()
show
hide
,每个按钮都位于各自的
中,其工作原理如下:

  • 默认情况下,
    显示
    按钮隐藏,主
    和隐藏按钮显示。单击
    隐藏
    按钮隐藏主
    和“隐藏”按钮本身,显示
    显示
    按钮
  • 单击
    show
    按钮显示主
    ,“show”按钮显示
    hide
    按钮。我认为这可以通过使用类似
    document.getElementById(DIVid).style.visibility=“hidden”
    的方法实现
但是我该怎么做呢

$('#hidebutton').click(function() {
   $('#DIVid, #showbutton').show();
   $(this).hide();
});

$('#showbutton').click(function() {
   $('#DIVid, #hidebutton').show();
   $(this).hide();
});
或者,如果您愿意:

$('#hidebutton, #showbutton').click(function() {
    var show = $(this).is('#showbutton');
    $('#DIVid, #hidebutton').toggle(show);
    $('#showbutton').toggle(!show);
});
或者,如果您愿意:

$('#hidebutton, #showbutton').click(function() {
    var show = $(this).is('#showbutton');
    $('#DIVid, #hidebutton').toggle(show);
    $('#showbutton').toggle(!show);
});

为每个
定义一个类,并使其成为jQuery函数。这是示例代码:

<div class="a"></div>
<div class="a"></div>
<div class="a"></div>

Jquery code

$(".a").click(function(e){
your code goes here
}

Jquery代码
$(“.a”)。单击(函数(e){
你的密码在这里
}

为每个
定义一个类,并使其成为jQuery函数。这是示例代码:

<div class="a"></div>
<div class="a"></div>
<div class="a"></div>

Jquery code

$(".a").click(function(e){
your code goes here
}

Jquery代码
$(“.a”)。单击(函数(e){
你的密码在这里
}
请检查以下内容:

$(“#btnclk”)。单击(函数(){
如果($('.test')。是(':visible')){
$(“.test”).hide();
}否则{
$(“.test”).show();
}
});
第一组
第一组
或者,如果要使用JavaScript执行此操作,请使用
GetElementsByCassName
而不是
getElementById

请检查以下内容:

$(“#btnclk”)。单击(函数(){
如果($('.test')。是(':visible')){
$(“.test”).hide();
}否则{
$(“.test”).show();
}
});
第一组
第一组
或者,如果您想使用JavaScript,那么使用
getElementsByClassName
而不是
getElementById

简单jQuery:

$('.show_hide').click(function(){
  $(this).text( $(this).text()==='HIDE'?'SHOW':'HIDE' );
  $(this).next('.box_content').stop().slideToggle();
});
HTML:

简单jQuery:

$('.show_hide').click(function(){
  $(this).text( $(this).text()==='HIDE'?'SHOW':'HIDE' );
  $(this).next('.box_content').stop().slideToggle();
});
HTML:


提供您的HTML以便我们能够理解结构您应该创建一个提琴:提供您的HTML以便我们能够理解结构您应该创建一个提琴: