为iQuery方法.css()使用多个值会引发错误:'missing''。然而,它并不缺少

时间:2019-05-30 20:55:25

标签: javascript jquery css

错误表明我缺少括号。但是,语法与w3Schools和Mozilla相同。如果仅设置一种样式,但.css()起作用,但设置多于1种则中断。

jQuery

 <script>
      $(document).ready( function(){

          // event-handler to create new element
          function createContainer() {

              // variables store values from <select> element
              var elementType = $('select').val();

              // string containing markup for use with .append()
              var elementTypeStr = $(` <${elementType}> This is a ${elementType} </${elementType}> `);
              // this throws error, while setting single style doesn't
              $('#layout_demo').append( elementTypeStr );
              elementTypeStr.css("background-color":" blue" , "height": "100px");

          } // end createContainer()

          // ---------------------------- event handlers ---------------------

          $('#btn').click( createContainer );
          // button to delete latest element or selected element

      }); // end ready

  </script>

HTML:

<section id="form">
  <!-- select element --->
  <label for="container"> Please select type of container to add</label>
  <select id= "container">
    <option value= "div" > &ltdiv&gt </option>
    <option value= "p"> &ltp&gt </option>
    <option value= "section"> &ltsection&gt </option>

  </select>
  <!-- Seperate container to hold now elements -->

  <button id="btn" > Create Container </button>
</section>

<div id="layout_demo">



</div>

2 个答案:

答案 0 :(得分:2)

使用.css()时,您可以通过两种方式传递参数:逗号分隔的字符串或对象文字符号

因此:

.css("background-color", " blue").css("height", "100px");

或更确切地说:

.css({backgroundColor:"blue" , height:"100px"});

另外,不要使用$('select').val();,而是使用更严格的选择器(例如某些ID); 'select'可以在页面中任意选择。

jQuery($ => {

  const createElement = () => {

    const tagName = $('#element').val();
    
    return $(`<${tagName}/>`, {
      appendTo: '#layout',
      text: `This is a ${tagName}`,
      css: {
        backgroundColor: "blue",
        height: "100px"
      },
    });

  }

  $('#create').on('click', createElement);

});
<section id="form">
  <label>
    Please select type of container to add
    <select id="element">
      <option value="div">&ltdiv&gt</option>
      <option value="p">&ltp&gt</option>
      <option value="section">&ltsection&gt</option>
    </select>
  </label>
  <button id="create">Create Container</button>
</section>
<div id="layout"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

答案 1 :(得分:-1)

多个.css()属性必须位于一个对象内。

$(document).ready(function() {

  // event-handler to create new element
  function createContainer() {

    // variables store values from <select> element
    var elementType = $('select').val();

    // string containing markup for use with .append()
    var elementTypeStr = $(` <${elementType}> This is a ${elementType} </${elementType}> `);
    // this throws error, while setting single style doesn't
    $('#layout_demo').append(elementTypeStr);
    elementTypeStr.css({
      "background-color": " blue",
      "height": "100px"
    });

  } // end createContainer()

  // ---------------------------- event handlers ---------------------

  $('#btn').click(createContainer);
  // button to delete latest element or selected element

}); // end ready
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="form">
  <!-- select element --->
  <label for="container"> Please select type of container to add</label>
  <select id="container">
    <option value="div"> &ltdiv&gt </option>
    <option value="p"> &ltp&gt </option>
    <option value="section"> &ltsection&gt </option>

  </select>
  <!-- Seperate container to hold now elements -->

  <button id="btn"> Create Container </button>
</section>

<div id="layout_demo">



</div>