Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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 删除隐藏/显示不同元素的函数中的重复_Javascript_Jquery - Fatal编程技术网

Javascript 删除隐藏/显示不同元素的函数中的重复

Javascript 删除隐藏/显示不同元素的函数中的重复,javascript,jquery,Javascript,Jquery,单击royal\u edit\u按钮时,它会隐藏视图部分并显示编辑部分,royal\u edit\u按钮则相反 有没有一种简单的方法?我不想对每个元素重复相同的代码。也许这样看起来更好: jQuery("#royal_edit_button").click(function () { jQuery('#royal_edit_title_edit').show(); jQuery('#royal_edit_price_edit').show(); jQuer

单击
royal\u edit\u按钮
时,它会隐藏
视图
部分并显示
编辑
部分,
royal\u edit\u按钮
则相反


有没有一种简单的方法?我不想对每个元素重复相同的代码。

也许这样看起来更好:

jQuery("#royal_edit_button").click(function () {        
    jQuery('#royal_edit_title_edit').show();
    jQuery('#royal_edit_price_edit').show();
    jQuery('#royal_edit_desc_edit').show();
    jQuery('#royal_edit_title_view').hide();
    jQuery('#royal_edit_price_view').hide();
    jQuery('#royal_edit_desc_view').hide();
});     

jQuery("#royal_edit_button_back").click(function () {       
    jQuery('#royal_edit_title_edit').hide();
    jQuery('#royal_edit_price_edit').hide();
    jQuery('#royal_edit_desc_edit').hide();
    jQuery('#royal_edit_title_view').show();
    jQuery('#royal_edit_price_view').show();
    jQuery('#royal_edit_desc_view').show();
}); 

也许这样看起来更好:

jQuery("#royal_edit_button").click(function () {        
    jQuery('#royal_edit_title_edit').show();
    jQuery('#royal_edit_price_edit').show();
    jQuery('#royal_edit_desc_edit').show();
    jQuery('#royal_edit_title_view').hide();
    jQuery('#royal_edit_price_view').hide();
    jQuery('#royal_edit_desc_view').hide();
});     

jQuery("#royal_edit_button_back").click(function () {       
    jQuery('#royal_edit_title_edit').hide();
    jQuery('#royal_edit_price_edit').hide();
    jQuery('#royal_edit_desc_edit').hide();
    jQuery('#royal_edit_title_view').show();
    jQuery('#royal_edit_price_view').show();
    jQuery('#royal_edit_desc_view').show();
}); 

也许这样看起来更好:

jQuery("#royal_edit_button").click(function () {        
    jQuery('#royal_edit_title_edit').show();
    jQuery('#royal_edit_price_edit').show();
    jQuery('#royal_edit_desc_edit').show();
    jQuery('#royal_edit_title_view').hide();
    jQuery('#royal_edit_price_view').hide();
    jQuery('#royal_edit_desc_view').hide();
});     

jQuery("#royal_edit_button_back").click(function () {       
    jQuery('#royal_edit_title_edit').hide();
    jQuery('#royal_edit_price_edit').hide();
    jQuery('#royal_edit_desc_edit').hide();
    jQuery('#royal_edit_title_view').show();
    jQuery('#royal_edit_price_view').show();
    jQuery('#royal_edit_desc_view').show();
}); 

也许这样看起来更好:

jQuery("#royal_edit_button").click(function () {        
    jQuery('#royal_edit_title_edit').show();
    jQuery('#royal_edit_price_edit').show();
    jQuery('#royal_edit_desc_edit').show();
    jQuery('#royal_edit_title_view').hide();
    jQuery('#royal_edit_price_view').hide();
    jQuery('#royal_edit_desc_view').hide();
});     

jQuery("#royal_edit_button_back").click(function () {       
    jQuery('#royal_edit_title_edit').hide();
    jQuery('#royal_edit_price_edit').hide();
    jQuery('#royal_edit_desc_edit').hide();
    jQuery('#royal_edit_title_view').show();
    jQuery('#royal_edit_price_view').show();
    jQuery('#royal_edit_desc_view').show();
}); 

以下是一个可能的解决方案:

如果您有类似以下内容的html:

$("#royal_edit_button").click(function () {        
    $('[id ^=royal_edit_][id $=edit]').show();
    $('[id ^=royal_edit_][id $=view]').hide();
}); 

$("#royal_edit_button_back").click(function () {
    $('[id ^=royal_edit_][id $=view]').show();
    $('[id ^=royal_edit_][id $=edit]').hide();
}

Js fiddle:

这里有一个可能的解决方案:

如果您有类似以下内容的html:

$("#royal_edit_button").click(function () {        
    $('[id ^=royal_edit_][id $=edit]').show();
    $('[id ^=royal_edit_][id $=view]').hide();
}); 

$("#royal_edit_button_back").click(function () {
    $('[id ^=royal_edit_][id $=view]').show();
    $('[id ^=royal_edit_][id $=edit]').hide();
}

Js fiddle:

这里有一个可能的解决方案:

如果您有类似以下内容的html:

$("#royal_edit_button").click(function () {        
    $('[id ^=royal_edit_][id $=edit]').show();
    $('[id ^=royal_edit_][id $=view]').hide();
}); 

$("#royal_edit_button_back").click(function () {
    $('[id ^=royal_edit_][id $=view]').show();
    $('[id ^=royal_edit_][id $=edit]').hide();
}

Js fiddle:

这里有一个可能的解决方案:

如果您有类似以下内容的html:

$("#royal_edit_button").click(function () {        
    $('[id ^=royal_edit_][id $=edit]').show();
    $('[id ^=royal_edit_][id $=view]').hide();
}); 

$("#royal_edit_button_back").click(function () {
    $('[id ^=royal_edit_][id $=view]').show();
    $('[id ^=royal_edit_][id $=edit]').hide();
}

Js fiddle:

您可以将一个类应用于所有要隐藏的元素。然后使用jQuery隐藏该类中的所有元素。^-这样,再加上使用jQuery的
切换
功能,您可以将
.hide
一次应用于多个元素,您只需一次选择它们:
jQuery(“"皇家编辑"标题"编辑,"皇家编辑"价格"编辑,…).hide()但是给这些元素一个公共类可能更有意义。隐藏/显示节容器,而不是单个元素。如果没有两个分区,则可以创建它们。您可以将一个类应用于所有要隐藏的元素。然后使用jQuery隐藏该类中的所有元素。^-这样,再加上使用jQuery的
切换
功能,您可以将
.hide
一次应用于多个元素,您只需一次选择它们:
jQuery(“"皇家编辑"标题"编辑,"皇家编辑"价格"编辑,…).hide()但是给这些元素一个公共类可能更有意义。隐藏/显示节容器,而不是单个元素。如果没有两个分区,则可以创建它们。您可以将一个类应用于所有要隐藏的元素。然后使用jQuery隐藏该类中的所有元素。^-这样,再加上使用jQuery的
切换
功能,您可以将
.hide
一次应用于多个元素,您只需一次选择它们:
jQuery(“"皇家编辑"标题"编辑,"皇家编辑"价格"编辑,…).hide()但是给这些元素一个公共类可能更有意义。隐藏/显示节容器,而不是单个元素。如果没有两个分区,则可以创建它们。您可以将一个类应用于所有要隐藏的元素。然后使用jQuery隐藏该类中的所有元素。^-这样,再加上使用jQuery的
切换
功能,您可以将
.hide
一次应用于多个元素,您只需一次选择它们:
jQuery(“"皇家编辑"标题"编辑,"皇家编辑"价格"编辑,…).hide()但是给这些元素一个公共类可能更有意义。隐藏/显示节容器,而不是单个元素。如果没有两个分区,请创建它们。