jQurey之data

文章目錄

data

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>溫故而知"心"</title>
  <link rel="stylesheet" href="./index.css">
</head>

<body>
  <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  /*
  data jQuery dom :
    存信息存數據的狀態;
    不會在標籤中顯示出來;
    存什麼取出來的就是什麼;
  */
  $('.demo').data("name", 22);
  console.log($('.demo').data("name"));
  console.log(typeof $('.demo').data("name"));

  $('.demo').data("fruits", {
    name: 'banana',
    color: 'yellow'
  })
  console.log($('.demo').data("fruits"))
  console.log(typeof $('.demo').data("fruits"))

</script>


</html>

data的運用

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>溫故而知"心"</title>
  <style>
    .tp1 {
      display: none;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <div class="tp1">
      <p></p>
      <span></span>
      <button>add</button>
    </div>

    <p class="show">
      <span>sum </span>
      <span class="sum">0</span>
    </p>
  </div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  /*
    jQ的data運用
  */
  var shopArr = [
    {
      name: 'james solider',
      shopName: 'nike',
      price: 110,
      id: '1001'
    },
    {
      name: 'Rose crazyLight',
      shopName: 'adidas',
      price: 90,
      id: '2002'
    },
    {
      name: 'curry one',
      shopName: 'Under Armour',
      price: 120,
      id: '3003'
    }
  ]

  shopArr.forEach(function (ele, index) {
    var oCloneDom = $('.tp1').clone().removeClass('tp1');
    oCloneDom.data({
      id: ele.id,
      shopName: ele.shopName,
      price: ele.price
    })
      .find('p')
      .text(ele.name)
      .next()
      .text(ele.price);

    oCloneDom.insertBefore($('.show'));
  })

  $('.wrapper button').click(function () {
    $('.sum').text(+$('.sum').text() + $(this).parent().data('price'))
    console.log($(this).parent().data('id'))
  })
</script>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章