如何在 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>

更新時間:13-2-2020

13K+ 閱讀次數

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告