2014-03-07 75 views
1

我使用jQuery來檢查div滾動。它適用於window,但它不適用於特定的div。.scroll()不工作的jQuery

正常工作:

$(window).scroll(function() { 
    console.log('scroll happened'); 
}); 

不工作呢精細:

$('.ui-multiselect-checkboxes').scroll(function(){ 
    console.log('scroll happened'); 
}); 

HTML 我使用ERIC海因茲多選。所以滾動ul與類ui-multiselect-checkboxes目前。你可以看到活生生的例子here

+0

請嘗試我給你,我嘗試瞭解決方案,它的工作原理 –

回答

1

嘗試:

$(document).on('scroll', '.ui-multiselect-checkboxes', function(){ 
    console.log('scroll happened'); 
}); 

這是因爲這些元素是動態的多選插件創建。

+0

這是行不通的親愛的。即使我已經通過將文件更改爲正文進行檢查。 –

+0

你是否將這段代碼編寫在文檔就緒處理程序中。如果沒有,請準備好 – Ramesh

+0

其已準備好的文件。 –

1

創建委託人事件,因爲這些是由您的multiselect插件動態創建的。

$(document).on('scroll', '.ui-multiselect-checkboxes', function(){ 
    console.log('scroll happened'); 
}); 
0

嘗試是這樣的

$(document).ready(function(){ 

    $('.ui-multiselect-checkboxes').bind('scroll',chk_scroll); 
}); 


function chk_scroll(e) 
{ 
    var elem = $(e.currentTarget); 
    if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) 
    { 
     console.log("Scrolled"); 
    } 

} 
0

解決

<script> 
$(function() { 
    $(".ui-multiselect-checkboxes").on('scroll', function() { 
     console.log('scroll happened'); 
     }); 
}); 
</script> 

// OH我檢查其工作。現在

只是檢查的地方,你在這個HTML的頭具有的所有文件,只是內部的「多選的jQuery的Widget Demo_files」 FOLDER

再檢查終端也隨之創建一個文件夾和文件的地方// 新的解決方案

<!DOCTYPE html> 
<!-- saved from url=(0078)http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm --> 
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

<title>jQuery MultiSelect Widget Demo</title> 
<link rel="stylesheet" type="text/css" href="./jQuery MultiSelect Widget Demo_files/jquery.multiselect.css"> 
<link rel="stylesheet" type="text/css" href="./jQuery MultiSelect Widget Demo_files/style.css"> 
<link rel="stylesheet" type="text/css" href="./jQuery MultiSelect Widget Demo_files/prettify.css"> 
<link rel="stylesheet" type="text/css" href="./jQuery MultiSelect Widget Demo_files/jquery-ui.css"> 
<script type="text/javascript" src="./jQuery MultiSelect Widget Demo_files/jquery.js"></script> 
<script type="text/javascript" src="./jQuery MultiSelect Widget Demo_files/jquery-ui.min.js"></script> 
<script type="text/javascript" src="./jQuery MultiSelect Widget Demo_files/prettify.js"></script> 
<script type="text/javascript" src="./jQuery MultiSelect Widget Demo_files/jquery.multiselect.js"></script> 
<script type="text/javascript"> 
$(function(){ 
    $("select").multiselect(); 
}); 
</script> 
</head> 
<body id="test" onLoad="prettyPrint();" cz-shortcut-listen="true"> 

<h2>Basic Demos</h2> 

<p>Both multiselects are created with the following one-liner. Optgroup support is built in out of the box:</p> 
<pre class="prettyprint"><span class="pln">$</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"><br>&nbsp; &nbsp;$</span><span class="pun">(</span><span class="str">"select"</span><span class="pun">).</span><span class="pln">multiselect</span><span class="pun">();</span><span class="pln"> <br></span><span class="pun">});</span></pre> 

<h3>Basic</h3> 
<p> 
    <select title="Basic example" multiple="multiple" name="example-basic" size="5" style="display: none;"> 
    <option value="option1">Option 1</option> 
    <option value="option2">Option 2</option> 
    <option value="option3">Option 3</option> 
    <option value="option4">Option 4</option> 
    <option value="option5">Option 5</option> 
    <option value="option6">Option 6</option> 
    <option value="option7">Option 7</option> 
    <option value="option8">Option 8</option> 
    <option value="option9">Option 9</option> 
    <option value="option10">Option 10</option> 
    <option value="option11">Option 11</option> 
    <option value="option12">Option 12</option> 
    </select><button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" title="Basic example" aria-haspopup="true" style="width: 225px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>Select options</span></button> 
</p> 

<h3>With Optgroups</h3> 
<p>Click on an optgroup's heading to toggle the checked state of the entire group.</p> 
<p> 
    <select name="example-optgroup" multiple="multiple" size="5" style="display: none;"> 
    <optgroup label="Group One"> 
     <option value="option1">Option 1</option> 
     <option value="option2">Option 2</option> 
     <option value="option3">Option 3</option> 
    </optgroup> 
    <optgroup label="Group Two"> 
     <option value="option4">Option 4</option> 
     <option value="option5">Option 5</option> 
     <option value="option6">Option 6</option> 
     <option value="option7">Option 7</option> 
    </optgroup> 
    </select><button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 225px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>Select options</span></button> 
</p> 



<div class="ui-multiselect-menu ui-widget ui-widget-content ui-corner-all" style="width: 217px;"><div class="ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix"><ul class="ui-helper-reset"><li><a class="ui-multiselect-all" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#"><span class="ui-icon ui-icon-check"></span><span>Check all</span></a></li><li><a class="ui-multiselect-none" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#"><span class="ui-icon ui-icon-closethick"></span><span>Uncheck all</span></a></li><li class="ui-multiselect-close"><a href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#" class="ui-multiselect-close"><span class="ui-icon ui-icon-circle-close"></span></a></li></ul></div><ul class="ui-multiselect-checkboxes ui-helper-reset"><li class=" "><label for="ui-multiselect-0-option-0" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-0" name="multiselect_0" type="checkbox" value="option1" title="Option 1"><span>Option 1</span></label></li><li class=" "><label for="ui-multiselect-0-option-1" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-1" name="multiselect_0" type="checkbox" value="option2" title="Option 2"><span>Option 2</span></label></li><li class=" "><label for="ui-multiselect-0-option-2" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-2" name="multiselect_0" type="checkbox" value="option3" title="Option 3"><span>Option 3</span></label></li><li class=" "><label for="ui-multiselect-0-option-3" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-3" name="multiselect_0" type="checkbox" value="option4" title="Option 4"><span>Option 4</span></label></li><li class=" "><label for="ui-multiselect-0-option-4" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-4" name="multiselect_0" type="checkbox" value="option5" title="Option 5"><span>Option 5</span></label></li><li class=" "><label for="ui-multiselect-0-option-5" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-5" name="multiselect_0" type="checkbox" value="option6" title="Option 6"><span>Option 6</span></label></li><li class=" "><label for="ui-multiselect-0-option-6" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-6" name="multiselect_0" type="checkbox" value="option7" title="Option 7"><span>Option 7</span></label></li><li class=" "><label for="ui-multiselect-0-option-7" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-7" name="multiselect_0" type="checkbox" value="option8" title="Option 8"><span>Option 8</span></label></li><li class=" "><label for="ui-multiselect-0-option-8" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-8" name="multiselect_0" type="checkbox" value="option9" title="Option 9"><span>Option 9</span></label></li><li class=" "><label for="ui-multiselect-0-option-9" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-9" name="multiselect_0" type="checkbox" value="option10" title="Option 10"><span>Option 10</span></label></li><li class=" "><label for="ui-multiselect-0-option-10" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-10" name="multiselect_0" type="checkbox" value="option11" title="Option 11"><span>Option 11</span></label></li><li class=" "><label for="ui-multiselect-0-option-11" title="" class="ui-corner-all"><input id="ui-multiselect-0-option-11" name="multiselect_0" type="checkbox" value="option12" title="Option 12"><span>Option 12</span></label></li></ul></div><div class="ui-multiselect-menu ui-widget ui-widget-content ui-corner-all" style="width: 217px;"><div class="ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix"><ul class="ui-helper-reset"><li><a class="ui-multiselect-all" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#"><span class="ui-icon ui-icon-check"></span><span>Check all</span></a></li><li><a class="ui-multiselect-none" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#"><span class="ui-icon ui-icon-closethick"></span><span>Uncheck all</span></a></li><li class="ui-multiselect-close"><a href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#" class="ui-multiselect-close"><span class="ui-icon ui-icon-circle-close"></span></a></li></ul></div><ul class="ui-multiselect-checkboxes ui-helper-reset"><li class="ui-multiselect-optgroup-label "><a href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#">Group One</a></li><li class=" "><label for="ui-multiselect-2-option-0" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-0" name="multiselect_2" type="checkbox" value="option1" title="Option 1"><span>Option 1</span></label></li><li class=" "><label for="ui-multiselect-2-option-1" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-1" name="multiselect_2" type="checkbox" value="option2" title="Option 2"><span>Option 2</span></label></li><li class=" "><label for="ui-multiselect-2-option-2" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-2" name="multiselect_2" type="checkbox" value="option3" title="Option 3"><span>Option 3</span></label></li><li class="ui-multiselect-optgroup-label "><a href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/basic.htm#">Group Two</a></li><li class=" "><label for="ui-multiselect-2-option-3" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-3" name="multiselect_2" type="checkbox" value="option4" title="Option 4"><span>Option 4</span></label></li><li class=" "><label for="ui-multiselect-2-option-4" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-4" name="multiselect_2" type="checkbox" value="option5" title="Option 5"><span>Option 5</span></label></li><li class=" "><label for="ui-multiselect-2-option-5" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-5" name="multiselect_2" type="checkbox" value="option6" title="Option 6"><span>Option 6</span></label></li><li class=" "><label for="ui-multiselect-2-option-6" title="" class="ui-corner-all"><input id="ui-multiselect-2-option-6" name="multiselect_2" type="checkbox" value="option7" title="Option 7"><span>Option 7</span></label></li></ul></div><div class="ugdv_contextMenu" id="ugdv_myMenu" style="display: none;"><ul id="ugdv_contextMenu"><li id="ugdv_menuItem_google_docs">Open in Google Docs Viewer</li><li id="ugdv_menuItem_new_tab">Open link in new tab</li><li id="ugdv_menuItem_new_window">Open link in new window</li><li id="ugdv_menuItem_new_incognito">Open link in new incognito window</li><li class="ugdv_seperator"></li><li id="ugdv_menuItem_download_file">Download file</li><li id="ugdv_menuItem_copy">Copy link address</li><li id="ugdv_menuItem_editpdf">Edit PDF File on PDFescape.com</li></ul></div><div id="ugdv_jqContextMenu" style="display: none; position: absolute; z-index: 9999;"></div><div style="background-color: rgb(0, 0, 0); position: absolute; opacity: 0.2; z-index: 9998; display: none;"></div> 

<script> 
$(function() { 
$('ul').click(function(){ 
var className=$(this).attr("class"); 
}); 

    $("."+className).on('scroll', function() { 
     console.log('scroll happened'); 
     }); 
}); 
</script> 
</body></html> 

enter image description here

+0

是否也應該說明一下,一個道理?/ –

+0

它也沒有工作 –

+0

您已經添加類級手動,但它是動態 –