如何使用jQuery在data属性中添加文本?

时间:2015-08-07 06:53:48

标签: javascript jquery html5 css3

查看我的fiddle代码: http://jsfiddle.net/hassaan39/0kourse6/



* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}
.pin_head {
  background: #eee;
}
.pin_head:after {
  content: "";
  display: block;
  clear: both;
  height: 0;
}
.pin_head > div {
  float: left;
  margin-left: -1px;
  border-left: 1px solid #ddd;
}
.pin_head > div strong {
  font-size: 13px;
  padding: 0 10px;
  line-height: 50px;
  display: block;
}
.pin_head > div.pin_id strong {
  line-height: 50px;
  padding: 0;
  text-align: center;
}
.pin_head > div.pin_id {
  margin: 0;
  border-left: 0px none;
}
.pin_id {
  width: 7%;
  border-left: 0px none;
}
.pin_pro {
  width: 14%;
}
.pin_date {
  width: 7%;
}
.pin_fname {
  width: 12%;
}
.pin_lname {
  width: 12%;
}
.pin_email {
  width: 11%;
}
.pin_phone {
  width: 11%;
}
.pin_pass {
  width: 20%;
}
.pin_lists ul {
  padding: 0;
  margin: 0;
}
.pin_lists ul li {
  list-style: none;
  border-bottom: 1px solid #ddd;
  overflow: hidden;
}
.pin_lists ul li:last-child {
  border-bottom: 0px none;
}
.pin_lists ul li:nth-child(even) {
  background: #fdfbfc;
}
.pin_lists ul li:after {
  content: "";
  display: block;
  clear: both;
  height: 0;
}
.pin_lists ul li > div {
  float: left;
  margin-left: -1px;
  border-left: 1px solid #ddd;
  padding: 7px 5px;
  min-height: 77px;
  word-wrap: break-word;
  margin-bottom: -99999px;
  padding-bottom: 100004px;
}
.pin_delete {
  width: 6.7%;
}
.pin_delete a {
  display: block;
  text-transform: uppercase;
  text-align: center;
  line-height: 44px;
  font-weight: bold;
}
.pin_lists ul li .pin_id {
  font-weight: bold;
  line-height: 44px;
  text-align: center;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="pin_head">
  <div class="pin_id"><strong>Order ID</strong>

  </div>
  <div class="pin_pro"><strong>Product Name</strong>

  </div>
  <div class="pin_date"><strong>Dated</strong>

  </div>
  <div class="pin_fname"><strong>First Name</strong>

  </div>
  <div class="pin_lname"><strong>Last Name</strong>

  </div>
  <div class="pin_email"><strong>Email</strong>

  </div>
  <div class="pin_phone"><strong>Phone</strong>

  </div>
  <div class="pin_pass"><strong>Password</strong>

  </div>
</div>
<div class="pin_lists">
  <ul>
    <li>
      <div class="pin_id" data-col="Order ID">654</div>
      <div class="pin_pro" data-col="Product Name">Low Voltage</div>
      <div class="pin_date" data-col="Dated">2015-08-07</div>
      <div class="pin_fname" data-col="First Name">John</div>
      <div class="pin_lname" data-col="Last Name">Tait</div>
      <div class="pin_email" data-col="Email">johnson@mail.com</div>
      <div class="pin_phone" data-col="Phone">123-456-789</div>
      <div class="pin_pass" data-col="Password">passcode</div>
      <div class="pin_delete" data-col="Change Record"><a href="#">Edit</a>
      </div>
    </li>
    <li>
      <div class="pin_id" data-col="Order ID">654</div>
      <div class="pin_pro" data-col="Product Name">Low Voltage</div>
      <div class="pin_date" data-col="Dated">2015-08-07</div>
      <div class="pin_fname" data-col="First Name">John</div>
      <div class="pin_lname" data-col="Last Name">Tait</div>
      <div class="pin_email" data-col="Email">johnson@mail.com</div>
      <div class="pin_phone" data-col="Phone">123-456-789</div>
      <div class="pin_pass" data-col="Password">passcode</div>
      <div class="pin_delete" data-col="Change Record"><a href="#">Edit</a>
      </div>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

我想使用.each()函数jQuery在(.pin_lists li)data-col中添加来自(pin_head&gt; div)的文本。我在这里很难对数值进行编码。

请帮助我走好路。 :) 我是jQuery的新手。

4 个答案:

答案 0 :(得分:1)

我想您希望每列的标题都是每个单元格的属性data-col

您可以这样做:

$(document).ready(function() {
  var list = $('.pin_lists li');
  $('.pin_head div').each(function(item) {
    var _this = this;
    list.each(function(element) {
      $($(this).children()[item]).attr('data-col', $(_this).find('strong').text())
    })
  })
})
* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}
.pin_head {
  background: #eee;
}
.pin_head:after {
  content: "";
  display: block;
  clear: both;
  height: 0;
}
.pin_head > div {
  float: left;
  margin-left: -1px;
  border-left: 1px solid #ddd;
}
.pin_head > div strong {
  font-size: 13px;
  padding: 0 10px;
  line-height: 50px;
  display: block;
}
.pin_head > div.pin_id strong {
  line-height: 50px;
  padding: 0;
  text-align: center;
}
.pin_head > div.pin_id {
  margin: 0;
  border-left: 0px none;
}
.pin_id {
  width: 7%;
  border-left: 0px none;
}
.pin_pro {
  width: 14%;
}
.pin_date {
  width: 7%;
}
.pin_fname {
  width: 12%;
}
.pin_lname {
  width: 12%;
}
.pin_email {
  width: 11%;
}
.pin_phone {
  width: 11%;
}
.pin_pass {
  width: 20%;
}
.pin_lists ul {
  padding: 0;
  margin: 0;
}
.pin_lists ul li {
  list-style: none;
  border-bottom: 1px solid #ddd;
  overflow: hidden;
}
.pin_lists ul li:last-child {
  border-bottom: 0px none;
}
.pin_lists ul li:nth-child(even) {
  background: #fdfbfc;
}
.pin_lists ul li:after {
  content: "";
  display: block;
  clear: both;
  height: 0;
}
.pin_lists ul li > div {
  float: left;
  margin-left: -1px;
  border-left: 1px solid #ddd;
  padding: 7px 5px;
  min-height: 77px;
  word-wrap: break-word;
  margin-bottom: -99999px;
  padding-bottom: 100004px;
}
.pin_delete {
  width: 6.7%;
}
.pin_delete a {
  display: block;
  text-transform: uppercase;
  text-align: center;
  line-height: 44px;
  font-weight: bold;
}
.pin_lists ul li .pin_id {
  font-weight: bold;
  line-height: 44px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pin_head">
  <div class="pin_id"><strong>Order ID</strong>

  </div>
  <div class="pin_pro"><strong>Product Name</strong>

  </div>
  <div class="pin_date"><strong>Dated</strong>

  </div>
  <div class="pin_fname"><strong>First Name</strong>

  </div>
  <div class="pin_lname"><strong>Last Name</strong>

  </div>
  <div class="pin_email"><strong>Email</strong>

  </div>
  <div class="pin_phone"><strong>Phone</strong>

  </div>
  <div class="pin_pass"><strong>Password</strong>

  </div>
</div>
<div class="pin_lists">
  <ul>
    <li>
      <div class="pin_id">654</div>
      <div class="pin_pro">Low Voltage</div>
      <div class="pin_date">2015-08-07</div>
      <div class="pin_fname">John</div>
      <div class="pin_lname">Tait</div>
      <div class="pin_email">johnson@mail.com</div>
      <div class="pin_phone">123-456-789</div>
      <div class="pin_pass">passcode</div>
      <div class="pin_delete"><a href="#">Edit</a>

      </div>
    </li>
    <li>
      <div class="pin_id">654</div>
      <div class="pin_pro">Low Voltage</div>
      <div class="pin_date">2015-08-07</div>
      <div class="pin_fname">John</div>
      <div class="pin_lname">Tait</div>
      <div class="pin_email">johnson@mail.com</div>
      <div class="pin_phone">123-456-789</div>
      <div class="pin_pass">passcode</div>
      <div class="pin_delete"><a href="#">Edit</a>

      </div>
    </li>
  </ul>
</div>

答案 1 :(得分:1)

使用以下jQuery根据data-col div的内容在li上动态创建pin_head元素:

$(".pin_lists div").each(function() {
    $(this).attr("data-col", $($(".pin_head").children()[$(this).index()]).find("strong").text());
});

现场演示:

$(".pin_lists div").each(function() {
    $(this).attr("data-col", $($(".pin_head").children()[$(this).index()]).find("strong").text());
});
* {
    margin:0px;
    padding:0px;
    box-sizing:border-box;
}
.pin_head {
    background: #eee;
}
.pin_head:after {
    content:"";
    display:block;
    clear:both;
    height:0;
}
.pin_head > div {
    float:left;
    margin-left:-1px;
    border-left: 1px solid #ddd;
}
.pin_head > div strong {
    font-size: 13px;
    padding:0 10px;
    line-height:50px;
    display:block;
}
.pin_head > div.pin_id strong {
    line-height: 50px;
    padding: 0;
    text-align: center;
}
.pin_head > div.pin_id {
    margin:0;
    border-left:0px none;
}
.pin_id {
    width:7%;
    border-left: 0px none;
}
.pin_pro {
    width:14%;
}
.pin_date {
    width:7%;
}
.pin_fname {
    width:12%;
}
.pin_lname {
    width:12%;
}
.pin_email {
    width:11%;
}
.pin_phone {
    width:11%;
}
.pin_pass {
    width:20%;
}
.pin_lists ul {
    padding:0;
    margin:0;
}
.pin_lists ul li {
    list-style:none;
    border-bottom: 1px solid #ddd;
    overflow:hidden;
}
.pin_lists ul li:last-child {
    border-bottom:0px none;
}
.pin_lists ul li:nth-child(even) {
    background: #fdfbfc;
}
.pin_lists ul li:after {
    content:"";
    display:block;
    clear:both;
    height:0;
}
.pin_lists ul li > div {
    float:left;
    margin-left:-1px;
    border-left: 1px solid #ddd;
    padding:7px 5px;
    min-height:77px;
    word-wrap:break-word;
    margin-bottom: -99999px;
    padding-bottom: 100004px;
}
.pin_delete {
    width:6.7%;
}
.pin_delete a {
    display:block;
    text-transform:uppercase;
    text-align:center;
    line-height:44px;
    font-weight:bold;
}
.pin_lists ul li .pin_id {
    font-weight:bold;
    line-height:44px;
    text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pin_head">
    <div class="pin_id"><strong>Order ID</strong>

    </div>
    <div class="pin_pro"><strong>Product Name</strong>

    </div>
    <div class="pin_date"><strong>Dated</strong>

    </div>
    <div class="pin_fname"><strong>First Name</strong>

    </div>
    <div class="pin_lname"><strong>Last Name</strong>

    </div>
    <div class="pin_email"><strong>Email</strong>

    </div>
    <div class="pin_phone"><strong>Phone</strong>

    </div>
    <div class="pin_pass"><strong>Password</strong>

    </div>
</div>
<div class="pin_lists">
    <ul>
        <li>
            <div class="pin_id">654</div>
            <div class="pin_pro">Low Voltage</div>
            <div class="pin_date">2015-08-07</div>
            <div class="pin_fname">John</div>
            <div class="pin_lname">Tait</div>
            <div class="pin_email">johnson@mail.com</div>
            <div class="pin_phone">123-456-789</div>
            <div class="pin_pass">passcode</div>
            <div class="pin_delete"><a href="#">Edit</a>
            </div>
        </li>
        <li>
            <div class="pin_id">654</div>
            <div class="pin_pro">Low Voltage</div>
            <div class="pin_date">2015-08-07</div>
            <div class="pin_fname">John</div>
            <div class="pin_lname">Tait</div>
            <div class="pin_email">johnson@mail.com</div>
            <div class="pin_phone">123-456-789</div>
            <div class="pin_pass">passcode</div>
            <div class="pin_delete"><a href="#">Edit</a>
            </div>
        </li>
    </ul>
</div>

该实时演示会产生以下结果:

DOM

答案 2 :(得分:0)

在jQuery中你可以这样做:

$("#yourElement").data("yourKeyName", "yourValue")

有关其他信息,请访问文档的this site

答案 3 :(得分:0)

你可以做这样的事情

 var array=[];
$('.pin_head > div').each(function (index,obj) { 

   array[index]= $(this).text();

});

$('li>div').each(function (index,obj) { 
    var data= $(this).attr("data-col",array[index]);
});