Create DOM element from jQuery selector

后端 未结 4 1939
野性不改
野性不改 2021-01-13 08:48

I was wondering if it were possible to, instead of selecting an element from the DOM via a jQuery selector, if I can create one instead.

For example:



        
4条回答
  •  无人及你
    2021-01-13 09:15

    I don't see something out there that would create elements off of css selector syntax. JQuery does let you create and add elements, but the syntax is not based off of the css selectors as in your example. For instance to add a #hello div with class a and b after the #test div, you can use .after():

    $('#test').after("
    Hello
    ");​​​​​​​​​

    jsfiddle: http://jsfiddle.net/septerr/RmAW6/

    or

    $('#test').after('
    Hello
    ').attr('id', 'hello').addClass('a').addclass('b');​

    jsfiddle: http://jsfiddle.net/septerr/EQjLE/

提交回复
热议问题