Javascript如何获取某id下的TagName

2024-12-26 12:41:07
推荐回答(3个)
回答1:

某个标签的tagName取法有两种:
1、javascript: document.getElementById(‘id’).firstChild.tagName;
2、jQuery: $(‘#id’).children().first().prop(“tagName”);

示例代码如下:

//点击span等标签里面的内容后变成可编辑的输入框
function edit(obj, name, id) {
var that = $(obj);
//var tag = obj.firstChild.tagName;
var tag = that.children().first().prop("tagName");

if (typeof(tag) != "undefined" && tag.toLowerCase() == "input") {
return ;
}

//保存原有内容
var org = that.html();

var val = that.text();
var txt = $("");
txt.val(val);
that.html(txt);
txt.focus();

txt.blur(function() {
var new_val = $(this).val();
if (new_val != val && new_val.length > 0) {
$.ajax({
url: 'url',
type: 'post',
data: 'data',
success: function() {
that.html(new_val);
}
});
} else {
that.html(org);
}
})
}

回答2:

某个标签的tagName取法有两种:
1、javascript:
document.getElementById(‘id’).firstChild.tagName;
2、jQuery:
$(‘#id’).children().first().prop(“tagName”);
示例代码如下:
//点击span等标签里面的内容后变成可编辑的输入框
function
edit(obj,
name,
id)
{
var
that
=
$(obj);
//var
tag
=
obj.firstChild.tagName;
var
tag
=
that.children().first().prop("tagName");
if
(typeof(tag)
!=
"undefined"
&&
tag.toLowerCase()
==
"input")
{
return
;
}
//保存原有内容
var
org
=
that.html();
var
val
=
that.text();
var
txt
=
$("");
txt.val(val);
that.html(txt);
txt.focus();
txt.blur(function()
{
var
new_val
=
$(this).val();
if
(new_val
!=
val
&&
new_val.length
>
0)
{
$.ajax({
url:
'url',
type:
'post',
data:
'data',
success:
function()
{
that.html(new_val);
}
});
}
else
{
that.html(org);
}
})
}

回答3:

function fun(){

var omenu=document.getElementById("menu");
var olis=omenu.childNodes[0].childNodes;
for(var i=0;i<6;i++)
document.write(olis[i].tagName+" "+olis[i].childNodes[0].childNodes[0].nodeValue+"
");
}

输出:
LI 1
LI 2
LI 3
LI 4
LI 5
LI 6