prepend

UK [pri:'pend] US [pri:'pend]

vt. Consider in advance, plan in advance, plan in advance

jquery prepend() method syntax

Function: prepend() method inserts the specified content at the beginning of the selected element (still inside). The prepend() and prependTo() methods have the same effect. The differences are in the syntax: the position of the content and selectors, and the fact that the prependTo() function cannot be used to insert content.

Syntax: $(selector).prepend(content)

##Parameters:

ParameterDescriptioncontent Required. Specifies the content to be inserted (can include HTML tags).

Use functions to append content: Use functions to insert specified content at the beginning of selected elements.

Syntax: $(selector).prepend(function(index,html))

Parameters:

ParametersDescriptionfunction(index,html) Required. Specifies a function that returns the content to be inserted. index Optional. Accepts the index position of the selector. html Optional. Accepts the current HTML of the selector.

jquery prepend() method example

<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(){
  $("button").click(function(){
    $("p").prepend("<b>Hello world!</b> ");
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>在每个 p 元素的开头插入内容</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance