如何用 jQuery 在字串中連線變數?


利用 jQuery html() 方法,你可以在 jQuery 中輕鬆地將變數連線到字串。嘗試執行下面的程式碼,瞭解如何在 jQuery 中使用字串中的變數 -

示例

現場演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(function(){
      $("a").click(function(){
        var id= $(".id").html();
        $('.demo').html("<br><div class='new' id='" + id + "'>Welcome to </div>");            
      });
    });
    </script>
</head>
<body>
   <div class="wrap">
      <a href="#">Click me</a>
      <div class="demo"></div>
      <div class="id">Qries</div>
   </div>
</body>
</html>

更新於: 2020-02-13

7,000+ 瀏覽量

Kickstart your Career

透過完成課程獲得認證

開始
廣告