使用$()的差异.jQuery的

Tuk*_*nov 1 jquery

我想问一下使用$()或不使用的差异$().在两种情况下,它实际上是有效的.有什么不同我真的不明白.

jQuery的

var harf = $('<p>Hello there</p>').appendTo('body');
  $(harf).on('click', function(){
    alert('harf');
  });
Run Code Online (Sandbox Code Playgroud)

//

var harf = $('<p>Hello there</p>').appendTo('body');
  harf.on('click', function(){
    alert('harf');
  });
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 5

第一个是完全没用的.在jQuery对象中嵌入jQuery对象没有意义.

请注意,你可以链接调用,你真的不需要那个harf变量:

$('<p>Hello there</p>').on('click', function(){
    alert('harf');
}).appendTo('body');
Run Code Online (Sandbox Code Playgroud)