jQuery / javascript替换标签类型 [英] jQuery/javascript replace tag type

查看:141
本文介绍了jQuery / javascript替换标签类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以遍历所有td标记并将其更改为th? (等)。

Is there an easy way to loop through all td tags and change them to th? (etc).

我目前的做法是用th包装然后删除td,但后来我失去了其他属性等。

My current approach would be to wrap them with the th and then remove the td, but then I lose other properties etc.

推荐答案

完全未经测试,但给它一个旋转:

Completely untested, but giving this a whirl:

$("td").each(function(index) {
  var thisTD = this;
  var newElement = $("<th></th>");
  $.each(this.attributes, function(index) {
    $(newElement).attr(thisTD.attributes[index].name, thisTD.attributes[index].value);
  });
  $(this).after(newElement).remove();
});

我正在寻找它,我想不出它为什么会这样做的原因工作!

I'm looking and looking at it, and I can't think of a reason why it wouldn't work!

1)遍历每个td元素


2)创建一个新元素


3)对于每个td,循环遍历其每个属性


4)将该属性和值添加到新元素


5)一旦所有属性到位,在td之后立即将元素添加到DOM,并删除td

1) loop through each td element
2) create a new th element
3) for each of those td's, loop over each of its attributes
4) add that attribute and value to the new th element
5) once all attributes are in place, add the element to the DOM right after the td, and remove the td

编辑:工作正常: http://jsbin.com/uqofu3/edit

这篇关于jQuery / javascript替换标签类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆