2012-06-14 27 views
0

我使用這樣的條件,點擊頁面上的元素:如何查看頁面中的地點?

var findElem = function(elems, text) { 
    for (var i = 0; i < elems.length; i++) { 
     if (elems[i].textContent == text) { 
      return elems[i]; 
     } else { 
      var result = findElem(elems[i].children, text); 
      if (result != undefined) { 
       return result; 
      } 
     } 
    } 
    return; 
} 

switch (document.getElementById('my_id').value) { 
    case "1": 
     findElem(document.documentElement.children, "blabla1").click(); 
     break; 
    case "2": 
     findElem(document.documentElement.children, "blabla2").click(); 
     break; 
    default: 
     break; 
} 

我想多了一個條件添加到該代碼。

頁我的工作是這樣的:

<body id="something"> 

<div id="something2" class="container"> 
    <div id="header"> 
     <a class="hmm" href="somelink"></a> 
     <a class="aname" target="_blank" href="anotherlink"></a> 
    </div> 

    <div id="anotherthing" class="anotherthing2"> 

<div class="differentthing " style="left: 2px; "> 
    <div class="asdafad"></div> 
</div> 

我要看看如果differentthing在短期沒有style

我想要做這樣的事情:

switch (document.getElementById('my_id').value) { 
    case "1": 
     ---if differentthing has no style attrib then--- 
     findElem(document.documentElement.children, "blabla1").click(); 
     break; 

所以如果differentthing頁的部分是這樣的

<div class="differentthing "> 

然後做findElem.click的事情。

如果是<div class="differentthing " style="left: 2px; ">則什麼也不做。

我希望我能形容。我怎樣才能做到這一點?抱歉我的英文不好。

回答

2
switch (document.getElementById('my_id').value) { 
    case "1": 
     // getting an array of elements with "differentthing" class 
     var elements = document.getElementsByClassName("differentthing"); 

     // if there is just one and it does not have "style" attribute 
     if (elements.length == 1 && !elements[0].getAttribute("style")){ 
      // perform some action 
      findElem(document.documentElement.children, "blabla1").click(); 
     }   
     break; 
+0

不幸的是,IE瀏覽器無法正常工作,它的'getAttribute'版本相當破碎:http://jsbin.com/oqukec/2在IE7中失敗(大概是在早些時候),可以在IE8和IE9中使用。 –

+0

getElementsByClassName在IE中也不受支持 - http://caniuse.com/getelementsbyclassname – jbabey

+3

@ T.J。 Crowder我猜這可能是不相關的,因爲greasemonkey標籤 - 讓它在IE中運行是一個單獨的問題:) – Li0liQ

0

想看看一個元素是否沒有風格?

對於初學者來說,使用jQuery

var hasStyle = $(".differentthing").attr("style") 
if(hasStyle){/*...do your cool stuff...*/} 

的ATTR方法集或返回元素的屬性。對你而言,它就是風格屬性。

+2

請不要發佈jQuery解答非jQuery問題。 –

+0

@ T.J。爲什麼?這是一個論壇規則或什麼?我覺得我只是想幫助,因爲jQuery是有幫助的。 – LCIII

+0

因爲JavaScript!= jQuery。還有很多其他的庫,OP沒有說他們使用任何,他們顯然不*使用jQuery,並且有時候不適合加載重量級的東西(比如,像一個greasemonkey腳本)。 –

0

我認爲,你可以使用這個功能來獲得一個屬性,在這種情況下,風格

http://www.w3schools.com/dom/met_element_getattribute.asp

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
<body> 
    <p id="title" style="color:red">algo</p> 

</body> 
<script type="text/javascript"> 
    var prueba = document.getElementById('title').getAttribute('style'); 
    document.write(prueba); 
</script> 
</html> 

另一個例子:

switch (document.getElementById('my_id').value) { 
    case "1": 
    elements = document.getElementsByClassName('differentthing'); 
    for(x in elements){ 
     if(!x.getAttribute('style')){ 
      //dont have style attribute do something 
     } 
    } 
    findElem(document.documentElement.children, "blabla1").click(); 
    break; 
相關問題