我在javascript中有'n'个javascript函数,它几乎获取了一个元素并设置了它的显示属性。
function ShowDivforassignclick() {
document.getElementById("FollowupDiv").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
}
function HideDivforassignclick() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable2").style.display = 'block';
document.getElementById("ImageButtonDiv2").style.display = 'block';
document.getElementById("close1").style.display = 'block';
return false;
}
function HideDivforassignclick1() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable2").style.display = 'block';
document.getElementById("ImageButtonDiv2").style.display = 'block';
document.getElementById("close1").style.display = 'block';
return false;
}
function HideDiv() {
document.getElementById("adddiv").style.display = 'none';
}
function HideImageButtonDivcontactinfollowup() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable4").style.display = 'block';
document.getElementById("ImageButtonDiv1").style.display = 'block';
return false;
}
function HideImageButtonDivcontact() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable1").style.display = 'block';
document.getElementById("ImageButtonDiv1").style.display = 'block';
document.getElementById("close").style.display = 'block';
return false;
}
function HideImageButtonDivclose() {
document.getElementById("adddiv").style.display = 'block';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable1").style.display = 'none';
document.getElementById("ImageButtonDiv1").style.display = 'none';
document.getElementById("ImageButtonDiv2").style.display = 'none';
document.getElementById("close").style.display = 'none';
return false;
}
function HideImageButtonDivclose1() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable2").style.display = 'none';
document.getElementById("datatable").style.display = 'block';
document.getElementById("ImageButtonDiv1").style.display = 'none';
document.getElementById("ImageButtonDiv2").style.display = 'none';
document.getElementById("close1").style.display = 'none';
}
function HideImageButtonDivclose1forfollowup() {
document.getElementById("adddiv").style.display = 'block';
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable2").style.display = 'none';
document.getElementById("datatable").style.display = 'block';
document.getElementById("ImageButtonDiv1").style.display = 'none';
document.getElementById("ImageButtonDiv2").style.display = 'none';
document.getElementById("close1").style.display = 'none';
}
function HideImageButtonDivuser() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable2").style.display = 'block';
document.getElementById("ImageButtonDiv2").style.display = 'block';
document.getElementById("close").style.display = 'block';
document.getElementById("close1").style.display = 'none';
return false;
}
function HideImageButtonDivuser1() {
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable2").style.display = 'block';
document.getElementById("ImageButtonDiv2").style.display = 'block';
document.getElementById("close").style.display = 'none';
document.getElementById("close1").style.display = 'block';
return false;
}
function HideImageButtonDivuserinfollowup() {
document.getElementById("FollowupDiv").style.display = 'none';
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("ImageButtonDiv2").style.display = 'block';
return false;
}
function HideImageButtonDivforAdd() {
document.getElementById("ImageButtonDiv").style.display = 'none';
document.getElementById("datatable").style.display = 'none';
document.getElementById("adddiv").style.display = 'block';
document.getElementById("ctl00_ContentPlaceHolder1_ImgNoRecords").style.display = 'none';
return false;
}
function HideImageButtonDivforEdit() {
document.getElementById('ImageButtonDiv').style.display = 'none';
document.getElementById("datatable").style.display = 'none';
document.getElementById("adddiv").style.display = 'block';
return true;
}
function ShowImageButtonDiv() {
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
document.getElementById("adddiv").style.display = 'none';
return true;
}
function ShowImageButtonDivs() {
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
document.getElementById("adddiv").style.display = 'none';
document.getElementById("close").style.display = 'none';
document.getElementById("close1").style.display = 'block';
return true;
}
function ShowImageButtonDivss() {
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
document.getElementById("adddiv").style.display = 'block';
document.getElementById("close1").style.display = 'block';
return true;
}
function ShowImageButtonDivforfollowup() {
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
document.getElementById("adddiv").style.display = 'block';
return true;
}
function ShowImageButtonDiv1() {
document.getElementById("ImageButtonDiv1").style.display = 'block';
document.getElementById("datatable1").style.display = 'block';
document.getElementById("adddiv").style.display = 'none';
document.getElementById("ImageButtonDiv").style.display = 'none';
return true;
}
function showDisplaydiv() {
document.getElementById("ConfirmationPanel").style.display = 'block';
document.getElementById("datatable").style.display = 'block';
document.getElementById("ImageButtonDiv").style.display = 'block';
document.getElementById("adddiv").style.display = 'none';
return false;
}
我认为必须有一种非常简单的方法可以使用jquery将这些函数组合成一个函数。有什么建议吗?
答案 0 :(得分:2)
jQuery与选择器一起使用并返回一个元素数组。您只需传递选择所有元素的选择器并调用hide()
即可。假设你的所有元素都有“foo”CSS类,例如<div class='foo'/>
,你写$('.foo').hide()
。假设您有两个ID为“thing1”和“thing2”的元素。你的选择器会有所不同,但你仍然会在返回的元素数组上调用hide():$('#thing1, #thing2').hide()
。 show()
和hide()
修改幕后的显示属性,您也可以直接修改它,例如:$('#thing').css('display', 'block')
。查看jQuery API以获取更多详细信息。将jQuery添加到您的页面,在Firebug控制台中播放,选择元素,隐藏和显示它们。
答案 1 :(得分:2)
你有很多方法可以去。
首先,在jQuery中,您可以使用document.getElementbyId('something')
而不是$('#something')
。 jQuery方式不是使用style.display = block
和style.display = none
,而是:
$('#something').hide();
$('#something').show();
甚至:
$('#something').toggle();
接下来,不是通过各自的ID识别长项目列表,而是希望您的页面具有结构化,以便您可以按类或通过封闭容器进行选择。如果要隐藏十个元素,并且它们都在div
容器内,请不要按ID隐藏每个元素。选择容器:
$('#container').find('.setOne').hide(); // assume class "setOne" on all
// elements in a particular group
$('#container_of_datatable2').hide(); // if hiding the container works for you
$('#container_of_datatable2')
.find('table, div') // to hide tables or divs within the specific container
.hide();
如果容器方法不适合您(您的ID似乎几乎但不太适合固定模式),则可以设置包含选择器的数组,以便一起使用的ID。 / p>
var setOne = ['#datatable4','#adddiv','#imagebuttondiv','#etc'];
var setTwo = ['#something','#something-else','#etc2'];
然后使用这样的东西:
$( setOne.join(",") ).hide();
看起来您正试图将代码包装在具有有意义(对您)名称的函数中。但是这些函数名称似乎与您将要显示和隐藏的对象类型更紧密相关,而不是与业务规则相关。所以也许你可能有HideImageButtonDivclose1forfollowup()
而不是function beginFollowup(){
$( setOne.join(",") ).hide();
$( setFive.join(",") ).show();
...etc...
}
:
beginFollowup();
endFollowup();
if( something ) {
beginSomeOtherThing();
}
因此,当您对这些函数和行为进行实际排序时,您的代码将更加清晰:
{{1}}
一些让你入门的想法。
答案 2 :(得分:1)
这个怎么样:
/**
* Shows or hides elements specified by array of element IDs,
* @param bShow true if you want to show the elements, hide otherwise
*/
function showHide(arrElemIds, bShow) {
$.each(arrElemIds, function(idx, elemId) {
if(!!bShow) {
$("#" + elemId).show();
}else {
$("#" + elemId).hide();
}
}
}
然后用作:
showHide(["adddiv", "ImageButtonDiv"]); //hide
showHide(["datatable2", "ImageButtonDiv2", "close1"], true); // show
// ...and so on
编辑:
第二个想法,我宁愿你有单独的功能来显示和隐藏:
/**
* Shows specified by array of element IDs,
*/
function show(arrElemIds) {
$.each(arrElemIds, function(idx, elemId) {
$("#" + elemId).show();
}
}
/**
* Hides elements specified by array of element IDs,
*/
function hide(arrElemIds) {
$.each(arrElemIds, function(idx, elemId) {
$("#" + elemId).hide();
}
}
hide(["adddiv", "ImageButtonDiv"]); //hide
show(["datatable2", "ImageButtonDiv2", "close1"]); // show