Javascript hide show div - 默认情况下显示 div

标签 javascript arrays html

我创建了一个脚本来显示 div单击<a>标签。但是,我想知道是否有人有一个简单的解决方案来使用相同的脚本但允许选择 div默认显示?我的脚本:

var portletToHide = new Array(
    'p_p_id_1_WAR_webformportlet_INSTANCE_P0j8_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_RPu7_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_6a0F_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_Ta1B_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_Cg3y_');

for(a=0;a<portletToHide.length; a++){
    document.getElementById(portletToHide[a]).setAttribute('style', 'display:none; visibility:hidden;')
}

function display(id, id2){

    for(a=0;a<portletToHide.length; a++){
        document.getElementById(portletToHide[a]).setAttribute('style', 'display:none; visibility:hidden;')
    }

    document.getElementById(id).setAttribute('style', 'display:block; visibility:visible;')

    if(id2){
        document.getElementById(id2).setAttribute('style', 'display:block; visibility:visible;')
    }

    $target =$('#'+id);

    $('html, body').stop().animate({'scrollTop': $target.offset().top}, 900, 'swing');
}

最佳答案

看看是否可以使用“hidden”类来呈现 HTML。

索引.css

.hidden {
    display: none;
    /* no purpose combined with display none */
    /* visibility: hidden; */
}

index.js

var portletsToHide = [
    'p_p_id_1_WAR_webformportlet_INSTANCE_P0j8_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_RPu7_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_6a0F_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_Ta1B_',
   'p_p_id_1_WAR_webformportlet_INSTANCE_Cg3y_'];

function setPortletsVisibility( visible ) {

    for( var a=0; a < portletToHide.length; a++ ){
        setElementVisibility( portletToHide[a], visible );
    }
}

function setElementVisibilityById( elementID, visibility ) {

    var className = visible
        ? ""
        : "hidden";

    document.getElementById( elementID ).className = className;
}

function display(id, id2){

    setPortletsVisibility( true );

    setElementVisibilityById( id, true );

    if(id2){
        setElementVisibilityById( id2, true );
    }
}

function someFunction( id, id2 ) {

    display( id, id2 );

    $target = $( '#' + id );

    $('html, body').stop().animate({'scrollTop': $target.offset().top}, 900, 'swing');
}

// Default
setPortletsVisibility( false );

请注意,我不执行可能会造成干扰的 jQuery addClass/removeClass。

关于Javascript hide show div - 默认情况下显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22411209/

相关文章:

javascript - 在本地存储上保存背景颜色

javascript - 错误类型错误 :Cannot read property 'forEach' of undefined in Angular

javascript - Ember JS 和模板 "Template not found"

jquery - 从上一个下拉列表中选择后如何自动删除下一个下拉列表?

html - CSS 垂直对齐 float div

javascript - 无法让 "clipboard.js"工作

字符数组显示为空,试图附加第二个字符串

arrays - 从数组中的数组中提取值

javascript - 如何在数组数组中的相同索引处获取最大值?

javascript - 通过点击一些改变两个 "div"的颜色,通过Javascript编程