JavaScript学习(二)

来源:互联网 发布:bash 数组 编辑:程序博客网 时间:2024/06/05 05:05

<script type="text/javascript">
  function _(value){
    document.write(value+"<BR>");
  }
</script>
<pre>
只能用name的场合:
1,radio ,checkbox
2,input 的value传到服务器端,只能用name。
3,form表单,习惯上用name。
</pre>

<form>
 <input type=radio name="gender" value="1">1
 <input type=radio name="gender" value="2">2
</form>

==============================================================
<pre>
1,定义了id的标签可以被javascript方便的访问到。
</pre>
-----------------------------------------------------

<div id=div1 style="background-color:green">123<font>000</font>456</div>

-----------------------------------------------------
<BR>
<script type="text/javascript">
    _(div1);
    _(div1.innerHTML);
    _(div1.innerText);   //ff不支持
    _(div1.outerHTML);   //ff不支持。ie支持
</script>
<pre>
2,ff 如果多个标签id号一样,默认取第一个。
3,ie 默认取到的是数组,可以靠下标来取出每一个。
</pre>

<div id=div2 style="background-color:blue">22222</div>
<div id=div2 style="background-color:blue">33333</div>
<script type="text/javascript">
    _(div2);
    //_(div2.innerHTML);
    _("-->"+div2[0].innerHTML);
    _("-->"+div2[1].innerHTML);
</script>

==============================================================
<!--
form表单是个例外,靠name就可以方便的访问到。
-->
<form name=form1>
 <input name=input11 value="12345678">
 <input id=input22 value="87654321">
</form>
<script type="text/javascript">
//_(input22.value);//ff支持,ie不支持

_(form1)
_(document.all["form1"])
_(document.forms[0])
_(document.forms["form1"])
_(document.all("form1"))
_(document.all.form1)
_(form1.input11.value);
_(document.form1.input11.value);
_(window.document.form1.input11.value);
_(window.window.window.window.document.form1.input11.value);
_(this.document.form1.input11.value);
_(self.document.form1.input11.value);
</script>

==============================================================<BR>

<font id="font1" name="font1" style="background-color:blue">123</font>
<font name="font1111" style="background-color:blue">123456</font>

<BR>


<script type="text/javascript">
    //_(document.all["font1"]);//ff,ie都不支持,undefind
    //_(document.all('font1'))//ff undifined,ie null
    //_(document.all.font1)//ff,ie都不支持
    ////////////////////////////////////////////------------------------------------------------------------------------------
    //w3c规范中getElementsByName是按着name属性进行检索的,
    //而MS的IE却是按着id来检索。导致不能得到应该得到的Elements,
    _(document.getElementsByName("font1")); 
   
    _(document.getElementsByName("font1").innerHTML); 
    _(document.getElementsByName("font1")[0].innerHTML);  //ff 支持,ie undefined

    //alert(ret.length)//ff undifined,ie undifined
    //alert(document.getElementsByName("font1").innerText)//ff undifined,ie undifined
    //alert(document.getElementsByName("font1").length)//ff 1,ie 0
    //alert(document.getElementsByName("font1")[0].innerHTML)//ff支持,ie不支持
    //alert(document.getElementsByTagName("font")[0].innerHTML)  //ff,ie都支持
    //alert(document.getElementById("font1"));
</script>
==============================================================<BR>


<font id=font2 style="background-color:green"><span>456</span></font>
<font id=font22222 style="background-color:green"><span>789</span></font>
<BR>
<script type="text/javascript">
    _(document.all["font2"])//ff,ie都支持
    _(document.all["font2"].length)//ff,ie都支持
    _(document.all["font2"][0].innerHTML)//ff,ie都支持
    _(document.all["font2"][1].outerHTML)//ff不支持,ie都支持
  
    _(document.all("font2"))//ff,ie都支持
    _(document.all.font2)//ff,ie都支持
    _(font2[1].innerHTML) //ie支持,ff不支持
    _("--????????????????????????>"+document.getElementById('font2'));//ff 不支持 ie支持

</script>


<!--
HTML DOM 定义了多种查找元素的方法,
        除了 getElementById() 之外,还有 getElementsByName() 和 getElementsByTagName()。
不过,如果您需要查找文档中的一个特定的元素,最有效的方法是 getElementById()。


ie执行document.getElementById(elementName)的时候,
返回的是第一个name或者id等于elementName的对象,并不是按照ID来查找的。

在fireFox中不存在这样的问题,
fireFox执行document.getElementById(elementName)的时候只查找id等于elementName的对象,
如果不存在则返回null。

radio,input等场合只能用name

http://www.csask.com/blog/?p=51
http://www.w3school.com.cn/tiy/t.asp?f=hdom_document_getbyid
-->

<BR>
text1:
    <input name="t1" type="text" id="t2" value="111"/>
<br>
  text2:
    <input name="t2" type="text" id="t1" value="222"/>
<br>


<script type="text/javascript">
_(document.getElementById('t2').value);
_(document.getElementById('t1').value);

</script>

<script type="text/javascript">
    function getSomethingById(x) {
        if (typeof x == "string")
            return document.getElementById(x);
        return x;
    }

    function $()
    {
      var elements = new Array();
      for (var i = 0; i < arguments.length; i++)
      {
        var element = arguments[i];
        if (typeof element == 'string')
          element = document.getElementById(element);
        if (arguments.length == 1)
          return element;
        //The push() method adds one or more elements to the end of an array and returns the new length.
        elements.push(element);
      }
      return elements;
    }
  
//alert($("textName2","textName1").length)
</script>