簡體   English   中英

如何選擇除一個div以外的整個html

[英]How to select entire html except for one div

我正在嘗試禁用除一個div以外的滾動,這是一個模式彈出窗口。 在手機上,它超出了屏幕的底部,因此我只需要該div,我們將其稱為#div100

我確定一旦有了這個,只要模式關閉,我就可以解除綁定以重新啟用滾動。

如何禁用除一個div以外的滾動?

的HTML

<html>
    <body>
        <div id="container">
            <div id="div1">...</div>
            <div id="div100">...</div>
            <div id="div3">...</div>
        </div>
        <div id="upper-div">...</div>
    </body>
</html>

JS

$('.button').bind("click touchend", function(){
         $('div').not('#div100').bind('touchmove', false);
});

我當時想通過調用div而不是bodyhtml會更容易,但是我不介意使用bodyhtml會更容易

我建議采用一種略有不同的方法:選擇HTML querySelectorAll("*")每個元素都應執行此操作,然后遍歷並在每個元素上綁定“ touchmove”“ false”。 然后,選擇#div100,然后將其解除綁定。

您可能需要精確地調整應用“ querySelectorAll”的要素,因為將某些綁定到#div100祖先的東西可能傳播到#div100。 因此, document.querySelectorAll可能不是最好的地方,而是#div100的parentNode,因此只有#div100的兄弟姐妹可以得到該限制。 當然,使用jquery可以使用所需的任何方法選擇元素。 我只引用querySelectorAll,因為我經常在香草中使用它。

編輯:

嘗試這樣的事情:

    $("container").each(function(){
        $(this).bind('touchmove', false);
    }
    $("#div100").bind('touchmove', true);

或者,您可以執行以下操作:

    $("container").each(function(){
        if ($(this)!=$("#div100")){
            $(this).bind('touchmove', false);
        }
    }

兩種方法都應該起作用。 請原諒我的語法,我對jquery並不十分熟悉。

$('.button').bind("click touchend", function(){
     if($('div').attr("id") == '#div100')
        $('div').bind('touchmove', false);
     else
        $('div').bind('touchmove', true);

});

嘗試這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM