javascript - jQuery combine multiple elements - Stack Overflow

I was experimenting with jQuery and came across a question. Can I use an actual selector with existing

I was experimenting with jQuery and came across a question. Can I use an actual selector with existing element as a bined selector for jQuery?

So, suppose I've created a DIV element on-the-fly:

var $div = $('<div>')
               .css('position', 'absolute')
               .hide();    // Just to be short

$('body').append($div);

And I want to show it when user hovers over P elements / paragraphs (at the cursor position):

$('p').hover(function(e) {

    // Change the position of $div with regards of cursor position

    $div.show();
}, function(e) {
    $div.hide();
});

BUT also I want to apply this hover handlers to $div itself. So instead of duplicating my code, I want to do something like this:

$('p', $div).hover(...)

which will select $div element along with all P elements.

I know I can write functions separately and pass the names as arguments to hover function.

Is there any solution to this kind of situation in jQuery? or is there a more accurate solution?

I was experimenting with jQuery and came across a question. Can I use an actual selector with existing element as a bined selector for jQuery?

So, suppose I've created a DIV element on-the-fly:

var $div = $('<div>')
               .css('position', 'absolute')
               .hide();    // Just to be short

$('body').append($div);

And I want to show it when user hovers over P elements / paragraphs (at the cursor position):

$('p').hover(function(e) {

    // Change the position of $div with regards of cursor position

    $div.show();
}, function(e) {
    $div.hide();
});

BUT also I want to apply this hover handlers to $div itself. So instead of duplicating my code, I want to do something like this:

$('p', $div).hover(...)

which will select $div element along with all P elements.

I know I can write functions separately and pass the names as arguments to hover function.

Is there any solution to this kind of situation in jQuery? or is there a more accurate solution?

Share Improve this question edited Apr 14, 2015 at 16:37 damian 5,4445 gold badges37 silver badges64 bronze badges asked Jun 2, 2014 at 12:30 Karlen KishmiryanKarlen Kishmiryan 7,5626 gold badges37 silver badges46 bronze badges 3
  • 1 have a look at .add() api.jquery./add -- something like $("p").add($div) should do the trick – xec Commented Jun 2, 2014 at 12:32
  • Just FYI $('p', $div) will select p elements inside the $div. It is the same as $div.find('p');. It is not aggregating the two selectors. – Rory McCrossan Commented Jun 2, 2014 at 12:33
  • Thanks @RoryMcCrossan, I know that ;) – Karlen Kishmiryan Commented Jun 2, 2014 at 12:35
Add a ment  | 

2 Answers 2

Reset to default 6

You could use the jQuery add method:

$('p').add($div).hover(function(e) { ...

If you have multiple elements to bine you don't wan to do

$('p').add($div1).add($div2).add($div3).add($div4).add($div5).add($div6) ...

Instead you want to convert a JavaScript array into a jQuery object

$( $.map([x,y,z], a => [...$.makeArray(a)]) )

Source: Merging jQuery objects

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745164500a4614557.html

相关推荐

  • javascript - jQuery combine multiple elements - Stack Overflow

    I was experimenting with jQuery and came across a question. Can I use an actual selector with existing

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信