JavaScript隐藏和显示切换,隐藏DIV

JavaScript隐藏和显示切换,隐藏DIV,javascript,jquery,toggle,Javascript,Jquery,Toggle,对于下面的示例,如何通过隐藏“DivToLoggle”DIV来启动页面,因为它当前在默认情况下显示?我不想使用“display:none;”出于可访问性原因,在脚本之外。如何在启动时在脚本中隐藏“DivToLogle”? 谢谢 JavaScript隐藏和显示切换 函数toggleAndChangeText(){ $('#divtologle').toggle(); if($('divotoggle').css('display')=='none'){ $('#aTag').html('[+]S

对于下面的示例,如何通过隐藏“DivToLoggle”DIV来启动页面,因为它当前在默认情况下显示?我不想使用“display:none;”出于可访问性原因,在脚本之外。如何在启动时在脚本中隐藏“DivToLogle”? 谢谢


JavaScript隐藏和显示切换
函数toggleAndChangeText(){
$('#divtologle').toggle();
if($('divotoggle').css('display')=='none'){
$('#aTag').html('[+]Show text');
}
否则{
$('#aTag').html('[-]隐藏文本');
}
}

将显示或隐藏的内容。
只需使用jQuery,因为您已经在使用它了(这种方式不会阻止非JS用户看到该元素):

此外,对切换功能的一个小更新:

function toggleAndChangeText() {
    // because you're accessing this element more than once,
    // it should be cached to save future DOM look-ups
    var divToToggle = $('#divToToggle');
    divToToggle.toggle();
    // You're not changing the HTML, just the text, so use the
    // appropriate method (though it's a *minor* change)
    $('#aTag').text(function() {
        // if the element is visible change the text to
        // '...hide...'; if not, change the text to '...show...'
        return divToToggle.is(':visible') ? '[-] Hide text' : '[+] Show Text';
    });
}
参考资料:


只需在document.ready函数中使用hide函数即可

$(function(){
     $('#divToToggle').hide();
});

谢谢大卫和威利。我想要的是隐藏函数,但不确定如何正确地标记它,谢谢你提供的wirey代码片段。
function toggleAndChangeText() {
    // because you're accessing this element more than once,
    // it should be cached to save future DOM look-ups
    var divToToggle = $('#divToToggle');
    divToToggle.toggle();
    // You're not changing the HTML, just the text, so use the
    // appropriate method (though it's a *minor* change)
    $('#aTag').text(function() {
        // if the element is visible change the text to
        // '...hide...'; if not, change the text to '...show...'
        return divToToggle.is(':visible') ? '[-] Hide text' : '[+] Show Text';
    });
}
$(function(){
     $('#divToToggle').hide();
});