jQuery append() html syntax behavior

I am learning to use jquery and am making some test pages for practice. For this example, I use jquery to bind a function, "showAddToDb()", to a button. When that button is pressed, jquery should append the following html form code into the div labeled by the id "memberContent" (which I have left out here). $(document).ready(function() { $('#addToDb').bind('click',showAddToDb); }); function showAddToDb() { var form = $("
"); var formName = "
"; var formLocation = "
"; var formDes = "
"; var formPrice = "
"; var formSubmit = "
"; form.append(formName); form.append(formLocation); form.append(formDes); form.append(formPrice); form.append(formSubmit); $('#memberContent').append(form); } The code above works, but I struggled to get it working and my question is why it works. To get it to work, I had to change the first line of the function from `var form = "
";` to `var form = $("
");` I know that the dollar sign stands for a jquery call, but I'm not sure why I need that here in this particular case. Also, am I supposed to also wrap all the other vars with `$()` for appending? It works as is, but I have seen some examples online that do this without explaining why.

以上就是jQuery append() html syntax behavior的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准