最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a div with jquery on hover? - Stack Overflow

programmeradmin1浏览0评论

So I've got a pretty stupid question. Is it possible, to create a div on hover? I don't have access to the file that creates a list for me, so it creates this.

<ul class="class">
    <li>content</li>
    <li>content</li>
    <li>content</li>
</ul>

In this particular project, I can't edit that file directly. but I'd like to create a on the outside of each list item

   <div>stuff</div><li>content</li>

I know it's not an IDEAL setup, but it's the way I'd like to do it. Worse es to worse, I've got another way that WORKS, but I'd like -

something like

jQuery('#target').hover(function() {
    jQuery(this).find('<li>').echoBefore('<div class="class">stuff</div>')
    });

That would be my IDEAL solution to the sticky situation I've got :P

So I've got a pretty stupid question. Is it possible, to create a div on hover? I don't have access to the file that creates a list for me, so it creates this.

<ul class="class">
    <li>content</li>
    <li>content</li>
    <li>content</li>
</ul>

In this particular project, I can't edit that file directly. but I'd like to create a on the outside of each list item

   <div>stuff</div><li>content</li>

I know it's not an IDEAL setup, but it's the way I'd like to do it. Worse es to worse, I've got another way that WORKS, but I'd like -

something like

jQuery('#target').hover(function() {
    jQuery(this).find('<li>').echoBefore('<div class="class">stuff</div>')
    });

That would be my IDEAL solution to the sticky situation I've got :P

Share Improve this question edited Mar 14, 2012 at 6:14 Xhynk asked Mar 14, 2012 at 6:08 XhynkXhynk 13.9k8 gold badges34 silver badges70 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You will want to use the method append

like so

jQuery('#target').hover(function() {
    jQuery(this).find('li').append('<div class="class">stuff</div>')
});

if you want to access this div after appending it, you can use jquery's method of creating a new element like this

jQuery('#target').hover(function() {
    var div = jQuery('<div></div>');
    div.addClass('class').html('stuff');
    jQuery(this).find('li').append(div);
    div.html('this is updated html after appendage');
});

To create the DIVs on the outside of each LI you can use the following code:

jQuery(this).find('li').before('<div class="class">stuff</div>');

However, if it's all the same, I would remend creating those DIVs on the inside of the LIs using this code:

jQuery(this).find('li').prepend('<div class="class">stuff</div>');

This would work:

$('#target').hover(function(){
    $(this).find('li').append('<div>stuff</div>');
});

But,when you hover multiple times, the div would be created multiple times.

发布评论

评论列表(0)

  1. 暂无评论