开发者

Declaring a variable with html in it, and inserting the variable by using .insertAfter

开发者 https://www.devze.com 2023-01-09 06:02 出处:网络
I was wondering if I could declare html as a variable.In other words, something like this: $(document).ready(function(){

I was wondering if I could declare html as a variable. In other words, something like this:

$(document).ready(function(){

  var Bold="Yes!  There is bold in the text a开发者_运维问答bove!!";
  var NoBold="No... There isn't any bolded text in the paragraph above..";
  var BoldButton="<input class="BoldButton" type="button" value="bold" id="WelcomeBoldButton">";

  $(BoldButton)
  .insertAfter('#intro');

});

And then, using the .insertAfter action, place it into my page at different intervals:

$(BoldButton).insertAfter(#'intro');

This doesn't appear to work, am I close to something though?


Your quotes are broken. Use ' within ", " within ', or escape the quotes.

$(document).ready(function(){

  var bold="Yes!  There is bold in the text above!!";
  var noBold="No... There isn't any bolded text in the paragraph above..";
  var $button = $("<input class='BoldButton' type='button' value='bold' id='WelcomeBoldButton'>");
  $('#intro').after( $button );
});

$button.insertAfter( $('#intro') ) would also work.


Your quotation mark is misplaced.

This:

$(BoldButton).insertAfter(#'intro');

should be:

$(BoldButton).insertAfter('#intro');

If you want only double quotes, you can escape them like this:

var BoldButton="<input class=\"BoldButton\" type=\"button\" value=\"bold\" id=\"WelcomeBoldButton\">";
0

精彩评论

暂无评论...
验证码 换一张
取 消