jquery before() method
Translation results:
before
English[bɪˈfɔ:(r)] US[bɪˈfɔr,-ˈfor]
prep.Before; precede, be superior to; when In front of...; instead of...
conj.before...;before...;earlier than...;with...
adv.before, before; in front, in front
jquery before() methodsyntax
Function: before() method inserts the specified content before the selected element.
Syntax: $(selector).before(content)
Parameters:
Parameters | Description |
content | Required. Specifies the content to be inserted (can include HTML tags). |
Use functions to insert content: Use functions to insert content before the specified element.
Syntax: $(selector).before(function(index))
Parameters:
Parameters | Description |
function(index) | Required. Specifies a function that returns the content to be inserted. |
index | Optional. Receives the index position of the selector. |
jquery before() methodexample
<html> <head> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ $("p").before("<p>Hello world!</p>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button class="btn1">在每个段落前面插入新的段落</button> </body> </html>
Click the "Run instance" button to view the online instance