我知道我们可以通过使用nodeName来获取节点名称,但是也可以使用jquery设置nodeName吗?基本上我想将节点名称的大小从大写变为小写。
非常感谢
答案 0 :(得分:3)
我认为为了正确地执行此操作,您需要替换此标记,如下所述: jQuery: how to change tag name?
不幸的是,这需要您替换整个元素而不仅仅是标记名称,因此如果要替换标记名称,则需要找到一种方法来保留标记的属性以及其中的代码。
幸运的是,我有一个额外的几分钟,所以我创建了这样做的功能:
function change_tag_name(selector,tagname){
var old_elm = $(selector);
//Create a jquery element based on selector
var original_elements = old_elm.get();
//Get the array of original dom objects
//Note: We get the original dom objects because jQuery objects do not provide access to the attributes array
for(var i=0; i< original_elements.length; i+=1){
var original_element = original_elements[i];
//Create reference to original element in dom
var new_elm = $(document.createElement(tagname));
//Create new element with desired tagname
new_elm.html($(original_element).html());
//Add original element's inner elements to new element
var original_attributes = original_element.attributes;
//Create an array of the original element's attributes;
var attributes = new Object();
//Create a new generic object that will hold the attributes for the new element
for(var j=0; j<original_attributes.length; j+=1){
var attribute = original_attributes[j];
var name = attribute.nodeName;
//Note: The attributes "nodeName","localName",and "name" appear to have the same value, so I just chose one. It's entirely possible that these could have different values in different contexts...
//var name = attribute.localName;
//var name = attribute.name;
var value = attribute.nodeValue;
//Note: The attributes "nodeValue","textContext",and "value" appear to have the same value, so I just chose one. It's entirely possible that these could have different values in different contexts...
//var value = attribute.textContext;
//var value = attribute.value;
attributes[name]=value;
}
//Populate attributes object
new_elm.attr(attributes);
//Assign attributes from old element to new element
$(original_element).replaceWith(new_elm);
//Replace old_element with new element
}
}
另外,我认为没有办法保证属性符合原始顺序。
答案 1 :(得分:0)
您无法更改nodeName,但您可以使用不同的名称创建一个新节点,复制属性,然后将其替换为另一个。
jQuery使得很难区分属性和属性,你可能能够使用IE专有的outerHTML属性(在其他一些浏览器中支持)和字符串操作,但这在一般情况下不适用于Web
如果有很多这样的节点,那么全局替换“