注册 登录
编程论坛 JavaScript论坛

javascript小白一枚 笔者的源代码有问题,苦于找不出问题所在。

Honor000 发布于 2017-02-28 21:48, 3384 次点击
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>热身<title>
</head>
<body>
  <p id="p1">我是第一段文字</p>
  <p id="p2">我是第二段文字</p>
        
  <script type="text/javascript">
  document.write("hello");
  document.getElementById("p1").style.color="green";
  document.getElementById("p2").style.color="red";
  </script>
</body>
</html>
只有本站会员才能查看附件,请 登录

笔者希望程序最后运行效果如上。

源代码如上
希望有人可以指点一二,笔者不胜感激。

[此贴子已经被作者于2017-2-28 21:54编辑过]

5 回复
#2
leeqihero2017-03-01 09:46
程序代码:
<html>
<head>
<meta charset="utf-8">
<script>
$=function(b){return document.getElementById(b)};_=function(){var b,a,f,e={},d=arguments.length;for(var c=0;c<d;c++){b=arguments[c];for(a in b){e[a]=b[a]}}return e};_$=function(){var b,a,e=arguments[0],d=arguments.length;for(var c=1;c<d;c++){b=arguments[c];for(a in e){if(e[a]===undefined||b[a]===undefined)continue;e[a]=b[a]}}return e};
</script>
</head>
<body>
<p id="p1">我是第一段文字</p>
<p id="p2">我是第二段文字</p>
</body>
<script>
document.write("hello");
_$($("p1").style,{color:"green"});
_$($("p2").style,{color:"red"});
</script>
</html>
只有本站会员才能查看附件,请 登录
#3
Honor0002017-03-01 13:53
回复 2楼 leeqihero
笔者属于javascript小白,您在二楼写的那些代码笔者看不太懂。
只有本站会员才能查看附件,请 登录

这张图片是源代码及其运行结果,笔者贴中问到的那个问题中的代码是我按照这张图片的源代码自己敲出来的,但是却没有办法形成正确的结果。我想请您帮我看一下我在贴中的代码和这张图片中可以运行出正确结果的代码的差别所在。
附上图中源代码如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>热身</title>
</head>
<body>
  <p id="p1">我是第一段文字</p>
  <p id="p2">我是第二段文字</p>
  
  <script type="text/javascript">
  document.write("hello");
  document.getElementById("p1").style.color="blue";
  document.getElementById("p2").style.color="red";
  </script>
</body>
</html>

[此贴子已经被作者于2017-3-1 13:58编辑过]

#4
leeqihero2017-03-09 08:25
JavaScript放在</body>后面
#5
zb19942018-03-01 12:46
<title>热身<title>
改为<title>热身</title>
#6
drsn5182019-09-23 18:32
<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>热身
    </title>
</head>

<body>
    <p id="p1">我是第一段文字</p>
    <p id="p2">我是第二段文字</p>

    <script type="text/javascript">
        document.write("hello");
        var p1 = document.getElementById("p1")
        var p2 = document.getElementById("p2")
        p1.style.color = "green";
        p2.style.color = "red"
    </script>
</body>
</html>
1