Use JQuery to build an anchor

前端 未结 5 1291
抹茶落季
抹茶落季 2020-12-20 11:19

I want to use jquery to build HTML like this:

  • Track Nam
  • 5条回答
    •  [愿得一人]
      2020-12-20 12:01

      There are several ways to do it, including (but not limited to):

      // one big string
      $('Track Name')
      
      // create  then append() the span
      $('').attr("href","#")
                  .append('Track Name');
      
      // create  then set all of its contents with html()
      $('').attr("href","#")
                  .html('Track Name');
      
      // append spans created earlier:
      var spans = $('Track Name');
      var a = $('').append(spans);
      

      Note that .html() replaces any and all existing contents, while .append() adds to the end. So given that you have two span elements in your example you could create those independently and append them one at a time:

      $('').append('')
                           .append('Track Name');
      

    提交回复
    热议问题