【问题标题】:Jquery - Insert value into h5 dynamically from div textJquery - 从 div 文本动态地将值插入 h5
【发布时间】:2019-03-20 02:45:26
【问题描述】:

从 div 中获取文本值并通过 jquery 插入到标签中。得到未捕获的错误:语法错误,无法识别的表达式:错误。

硬编码值可以正常工作。

JS:

$('.select-destination').on('click', function () {
 let getDiscovery = $('.findTxt').text();
 //it works fine $('<h5>Find other DISCOVERY hotels</h5>').insertBefore($('.group-result.gha-group:first'));
 $("'<h5>'+getDiscovery+'</h5>'").insertBefore($('.group-result:first'));
});

HTML:

<div class="findTxt hidden">Find other Hotels</div>

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    这里的错字或错误 - 应该是:

    $('<h5>'+getDiscovery+'</h5>').insertBefore($('.group-result:first'));
    

    【讨论】:

      【解决方案2】:

      $('.select-destination').on('click', function() {
        let getDiscovery = $('.findTxt').text();
        $("<h5>" + getDiscovery + "</h5>").insertBefore($('.group-result:first'));
      });
      <button class="select-destination">Click</button>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <div class="findTxt hidden">Find other Hotels</div>
      <h5 class="group-result"></h5>
      <h5 class="group-result"></h5>
      <h5 class="group-result"></h5>

      【讨论】:

        【解决方案3】:

        其实下面语句中的引号有语法错误

         $("'<h5>'+getDiscovery+'</h5>'").insertBefore($('.group-result:first'));
        

        我已更正引号中的语法错误。请参考此代码来解决此问题。

        $('.select-destination').on('click', function() {
          let getDiscovery = $('.findTxt').text();
          $("<h5>" + getDiscovery + "</h5>").insertBefore($('.group-result:first'));
        });
        <button class="select-destination">Click</button>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <div class="findTxt hidden">Find other Hotels</div>
        <h5 class="group-result"></h5>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-30
          • 1970-01-01
          • 1970-01-01
          • 2012-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-25
          相关资源
          最近更新 更多