2013-01-22 115 views
0
function handle_node(entity) 
{ 
    var i = 0; 

    var varName = window.event.srcElement.name.substring(0,7); 
    var fieldValue = window.event.srcElement.value; 
    var fieldName; 
    // traverse thru all the products in the family 
    for(i=0; i < entity.childNodes.length; i++) 
    { 
     if(entity.childNodes(i).tagName == "DIV") 
     { 
      handle_child_node(entity.childNodes(i)) 
     } 
    } 
} 

function handle_child_node(entity) 
{ 
    var it = 0; 
    var oObject = entity.all; 

    if (oObject != null) 
    { 
     if (oObject.length != null) 
     { 
      for (it = 0; it < oObject.length; it++) 
      { 
       if (oObject(it).tagName == 'INPUT' && oObject(it).attributes["type"].nodeValue == 'checkbox') 
       { 
        resetTextFieldValue(window.event.srcElement, oObject(it)); 
       } 
      } 
     } 
    } 
} 

上述代碼在IE中正常工作。但它不適用於Mozilla。然後我改變下面給出的代碼。但它沒有得到div標籤下的標籤。代碼可以在Mozilla上進行哪些更改?如何在div標籤下使用javascript在Mozilla中獲取所有標籤

function handle_node(entity) 
     { 
      var i = 0; 
     if (entity.hasChildNodes()) 
     { 
     children= entity.childNodes; 
     for(i=0; i < children.length; i++) { 

      var sibling= children[i]; 

       if(sibling.tagName == "DIV") { 

      var elms = document.getElementsByTagName(sibling); 
       handle_child_node(sibling) 
       } 
      } 
     } 
     } 

     function handle_child_node(entity) 
     { 
     alert("entity"+entity); 
      var it = 0; 
      var oObject = entity.all; 
      if (oObject != null) 
      { 
       if (oObject.length != null) 
       { 
        for (it = 0; it < oObject.length; it++) 
        { 
         if (oObject(it).tagName == 'INPUT' && oObject(it).attributes["type"].nodeValue == 'checkbox') 
         { 
          resetTextFieldValue(window.event.srcElement, oObject(it)); 
         } 
        } 
       } 
      } 
    } 

在句柄handle_child_node(兄弟)正確嗎?我認爲var oObject = entity.all;不工作。

+0

你能設置一個[的jsfiddle(http://jsfiddle.net),所以我們可以看到如何綁定事件處理程序,因爲該處理程序也需要一些工作 –

回答

0

您的handle_node函數看起來像MicroSoft的ECMAScript(JScript)子/超集中的事件處理程序。幸運的是,事件模型是X瀏覽器代碼問題的主要來源。你的代碼在我看來就是典型的JScript。查看有關quirksmode事件的文章,以更好地瞭解/瞭解這些差異。
現在,雖然,這裏是你應該知道的:

JScript不活動對象的實例傳遞到處理程序,但全球對象 - window - 有一個名爲event財產,而所有其他瀏覽器執行將事件對象傳遞給處理程序。一般情況下,你會看到處理程序看起來像這樣:

document.getElementById('foo').onclick = bar;//bar is handler 
function bar(e) 
{ 
    e = e || window.event;//use passed event instance, or get event property for IE 
    var element = e.target || e.srcElement;//the reference to the DOM element is assigned to another property in JScript 
    //a lot of stuff 
    if (e.preventDefault) 
    {//w3c's events are "controlled" with these methods 
     e.preventDefault(); 
     e.stopPropagation(); 
    } 
    e.returnValue = false;//IE's jScript, but W3C engines have these properties, too 
    e.cancelBubble = true; 
} 

什麼這些方法和屬性意味着(如果你不知道)是你可以在怪異模式瞭解。
簡而言之:您的代碼適用於IE,因爲您使用的是在JScript中引用DOM節點的屬性,並且您假定事件對象是全局引用,但FF,Chrome/Chromium中並不是這種情況, Safari,Opera ...所以你需要先解決這個問題。

之後,要獲取div中的所有子節點,您根本不需要children屬性。更重要的是:你不應該依賴這個屬性:Mozilla的引擎將把空白列爲孩子。 Here's a list of references to children, and the X-browser differences

基本上,你真正需要做的是這樣的:

var children = divReference.getElementsByTagName('*'); 

,你就會得到你永遠需要的所有引用一個NodesList對象...

0

什麼這個想法

function FindTags() 
     { 
      var childDiv = document.getElementById("yourdiv").childNodes; 
      for (i = 0; i < childDiv.length; i++) 
      { 
       if (childDiv[i].tagName == "A") 
       { 
        childDiv[i].style.display = "block"; 
       } 
       if (childDiv[i].tagName == "SPAN") 
       { 
        childDiv[i].style.fontWeight = "normal"; 
       } 
//and so on for all tags you can do any thing 
      } 
     } 

希望這helps.enjoy

+0

'FindTags'不是構造函數。約定規定構造函數以大寫字母開頭,而常規函數以小寫字母開頭。使用包含'child'的屬性也有風險(請參閱我的答案)。你也在for循環中創建了一個全局變量('i'),並且我會在循環內部使用'switch'或者-attane-else if:如果'tagName'是''A' ',沒有必要檢查'tagName =='DIV''也是...對於挑剔的對不起... ...但只有很多小的東西可能會派上用場 –

相關問題