Mouseenter事件处理程序不适用于li

时间:2017-09-07 10:03:23

标签: javascript jquery html css

当我尝试将mouseenter事件处理程序添加到li标签时,它显示错误:

  

未捕获的TypeError:list.mouseenter不是函数

var data = new FormData();
data.append('action', 'uploadFile');

var request = new XMLHttpRequest();
request.open('POST', 'channelEdit.php');
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //<------Add this line

request.onreadystatechange = function(){
    if(request.readyState == 4){
        try {
            var resp = JSON.parse(request.response);
        } catch (e){
            var resp = {
                status: 'error',
                data: 'Unknown error occurred: [' + request.responseText + ']'
            };
        }
        console.log(resp.status + ': ' + resp.data);
    }
};    

request.send(data);

    $(this).unbind('click').click(function (e) {
    });


$( "#fileList" ).trigger( "click" );
var list = $('.side_bar_pages ul li')[1];

list.mouseenter(() => {
  console.log('hello world')
})

3 个答案:

答案 0 :(得分:2)

问题是因为您通过索引访问jQuery对象,该对象返回Element对象。这没有XmlWriter函数 - 因此错误。

如果要访问集合中的第二个mouseenter并返回jQuery对象,请改为使用lieq()

&#13;
&#13;
:eq()
&#13;
var list = $('.side_bar_pages ul li').eq(1);
// var list = $('.side_bar_pages ul li:eq(1)'); // alternate logic, same result

list.mouseenter(() => {
  console.log('hello world')
})
&#13;
&#13;
&#13;

答案 1 :(得分:1)

当您选择[1]元素时,您不需要<li>。删除它,你的代码工作

var list = $('.side_bar_pages ul li')

list.mouseenter(() => {
  console.log('hello world')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="side_bar_pages">
  <ul>
    <li id="clicked_secton">
      <span><img src="/icons/home.svg"></span>
      <a href="/">home</a>
    </li>
    <li>
      <span><img src="/icons/videocamera.svg"></span>
      <a href="/footages">stock footage</a>
    </li>
    <li>
      <span><img src="/icons/photocamera.svg"></span>
      <a href="/images">stock photos</a>
    </li>
    <li>
      <span><img src="/icons/hot.svg"></span>
      <a href="/new">hot stock</a>
    </li>
  </ul>
</section>

答案 2 :(得分:0)

其他选择是像css一样使用nth-child

var li = $('.side_bar_pages ul li:nth-child(1)');

li.on("mouseover", function(){
    console.log("entered");
})