Clearest way to build html elements in jQuery

前端 未结 10 1175
一个人的身影
一个人的身影 2020-12-12 11:12

I\'ve seen a lot of different styles (and few different methods) of creating elements in jQuery. I was curious about the clearest way to build them, and als

10条回答
  •  抹茶落季
    2020-12-12 12:14

    Templates are great and if you have access to them in your project, I suggest you use them. If you're using Underscore or Lodash it's built in. In some cases however, you will need to build HTML in your code whether it's refactoring or testing. I've found that the below format is the clearest to read when that is the requirement.

    Note: The HTML spec allows single OR double quotes for attributes in your markup so don't bother with all the crazy escaping.

    this.$fixture = $([
      "
    ", "
    ", "
    ", " ", "
    ", "
    " ].join("\n"));

提交回复
热议问题