2014-03-07 49 views
0

目前我使用JavaScript,HTML和jQuery工作。運行功能如果DIV元素不包含HTML標籤

比方說,我有一個表,如果我雙擊「TR」的元素,那麼它會運行一個函數(更新()),這將生成一個文本框。但是我有點困惑,關於如何在div中沒有​​html標籤的情況下運行函數(Update())。

這裏是我的代碼:

function update(id) { 
    for (i = 0, j = 8; i < j; i++) { 
     con = $('#div-' + i).html(); 
     if (con != "") { 
      $('#div-' + i).html("<input type = text class=erf id = gn_" + id + " value=" + con + ">") 
      if (i == 5 || i == 6) { 
       $(this).addAttr("disabled"); 
      } 
     } else { 
      if ($('#int_' + id).is(':checked')) { 
       $('.r1').html("<input name = tp class=tp id = int type =radio checked>") 
      } else { 
       $('.r1').html("<input name = tp class=tp id = ext type =radio>") 
      } 

      if ($('#ext_' + id).is(':checked')) { 
       $('.r2').html("<input name = tp class=tp id = int type =radio checked>") 
      } else { 
       $('.r2').html("<input name = tp class=tp id = ext type =radio>") 
      } 

      if ($('#dp_' + id).is(':checked')) { 
       $('.r3').html("<input name = tp class=tp id = int type =radio checked>") 
      } else { 
       $('.r3').html("<input name = tp class=tp id = dp type =radio>") 
      } 
     } 
    } 
    $('#divb-' + id).html("<input type = button value = Update class= 'button-mid ch' id=" + col[8] + ">") 
} 

感謝。

+0

而現在,您檢查innerHTML是否具有長度的方式不起作用。 – adeneo

+0

你能否包含一段html代碼?下面的答案是檢查是否在div中沒有​​任何HTML元素的一個完美的方式...「選擇」是到div,而不是TR參考......不知道爲什麼它被向下投 – Ted

+0

@ted是啊,答案是完美的,爲我工作。謝謝:D – user1814734

回答

-1

您可以檢查所有的子元素的length。如果它是0,則不存在子元素HTML元素:

if ($("selector").children().length == 0) 
+0

謝謝。有用! – user1814734