3088569说错了,在JavaScript里面事件名是 onXXX(你百度一下“不喜欢onXXX”,就知道了,呵呵)
<html>
<head>
<title>添加onclick事件</title>
<script language="javascript" type="text/javascript">
windowonload = function() {
var tagP = documentgetElementsByTagName("p");// 获取所有P标签元素
for (var i = 0, len = tagPlength; i < len; i++) {
var thisID = tagP[i]id;
tagP[i]onclick = function() {// 给P标签添加onclick事件
alert(thisID);
};
}
}
</script>
</head>
<body>
<p id="1">1111111111</p>
<p id="2">2222222222</p>
</body>
</html>
对了,看了我为ぁ琴狂的回答才发觉,你是要d出ID的吧?
但你自己写的那里面确是里面的内容,如果你要的是内容的话,把var thisID = tagP[i]id;改一下,改成tagP[i]innerHTML就可以了。
// 纯js这样写,要是jquery的话 就方便多了 rurl = $("link[rel=canonical]")attr("href");
var rurl = "";
var links = documentgetElementsByTagName("link");
var link = {};
for(var i=0;i<linkslength;i++){
link = links[i];
if(linkrel === "canonical"){
rurl = linkhref;
break;
}
}
alert(rurl);
自己试试吧:
<!doctype html public "-//w3c//dtd html 40 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<style>
body{
background-color:red;
}
divsel{
font-size:15px;
color:green;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
windowonload = function(){
var sheet =documentstyleSheets[0];
var rules = sheetcssRules || sheetrules;//
var rule = rules[0];
alert(ruleselectorText)
alert(rulestylecssText)
rule = rules[1];
alert(ruleselectorText)
alert(rulestylecssText)
}
//-->
</SCRIPT>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
</style>
<script type="text/javascript">
var array = [];
onload = function ()
{
var tags = documentbodygetElementsByTagName ('');
for ( var i = 0; i < tagslength; i++)
{
arraypush (getText (tags[i]));
}
consolelog (array);
}
var getText = function (dom)
{
var index = 0, html = dominnerHTML;
while (domchildrenlength && index < domchildrenlength)
{
var chtml = domchildren[index]outerHTML;
html = dominnerHTMLreplace(chtml, '');
index++;
}
return "<font>" + html + "</font>";
}
</script>
</head>
<body>
<p>
ths is p<a href="">this is a</a>wyz
</p>
<i>this is i</i>
<b>this is b</b>
</body>
</html>
以上就是关于js获取点击的标签id的疑问 不用jQuery全部的内容,包括:js获取点击的标签id的疑问 不用jQuery、使用js获得link标签的一个herf值该怎么写、怎么用JS获取到style标签里的内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)