2013-05-14 89 views
1

我的HTML是:尋找特定代碼的某個

<a id="showSlotsByLocation_" href="#" style="color:blue;" onclick="confirmAppt('28/05/2013','364301');">14.00 - 14.15</a> 
<a id="showSlotsByLocation_" href="#" style="color:blue;" onclick="confirmAppt('28/05/2013','364303');">14.15 - 14.30</a> 

身份證姓名上的所有鏈接相同。這是主要困難。

我想點擊第二個鏈接我的javascript代碼是配置Web瀏覽器是

if (location.pathname == "/abc") 
{ 
    //alert('location found') this is ok found; 

    var el = document.getElementsByTagName("a"); 
    for (var i=0;i<el.length;i++) 
    { 
     if (el.id == 'showSlotsByLocation_' && el.innerText.isEqual('14.15 - 14.30') && el.outerHTML.contains("confirmAppt('28/05/2013'")) 
     { 
      alert('link found') \\this condition not match; 
      el.onclick(); 
     } 

    } 
} 

我做了什麼,以符合條件?

+1

是的isEqual'()'和'包括()'自定義函數,因爲我沒有這樣的事情本身的存在呢? – adeneo 2013-05-14 16:32:48

+0

在HTML頁面上有多個相同的ID是違反規範的,所以如果你負責HTML(或者知道這個人是誰),你應該改變它。其次,與@adeneo所說的一樣,你應該真的在做'el.innerText ==='.15 - 14.3''(以及在第一次比較中使用'==='來表示一致性/好的形式)。 – 2013-05-14 16:34:35

+0

一致性或好的形式與是否使用兩個或三個等號作爲比較運算符無關,是否匹配類型和值是唯一非常重要的事情,並且不使用三個等號到處都是一致的? – adeneo 2013-05-14 16:42:04

回答

3

你不能有兩個具有相同ID的元素,ID是唯一的。

當你將有改變的ID,你只需使用可以訪問它們document.getElementById('idOfYourElement')

編輯:所有的 首先,你需要聲明一個「當前」變量取當前元素的循環,您不能使用el.id,因爲el是HTMLElements的集合!對不起,我以前沒有注意到它。 所以,你需要這個(定義變量 for循環,只是if語句前):

var current = el[i]; 

現在您已經定義了它,改變用下面的代碼這一整條生產線。

if (el.id == 'showSlotsByLocation_' && el.innerText.isEqual('14.15 - 14.30') && el.outerHTML.contains("confirmAppt('28/05/2013'")) 

我認爲這是阻止你的代碼。在JS中沒有稱爲isEqualcontains的功能。

if (current.id == 'showSlotsByLocation_' && current.textContent === '14.15 - 14.30' && current.outerHTML.indexOf("confirmAppt('28/05/2013'") !== -1) 

最後一兩件事:的innerText不是有效的跨瀏覽器的性能,使用的textContent代替。

MDN Reference

更新JS代碼

if (location.pathname == "/abc") 
{  
    var el = document.getElementsByTagName("a"); 
    for (var i=0;i<el.length;i++) 
    { 
     var current = el[i]; 
     if (current.id == 'showSlotsByLocation_' && current.textContent === '14.15 - 14.30')//I'm not sure about this one, in case you want it just remove the comment and the last parenthesis && current.outerHTML.indexOf("confirmAppt('28/05/2013'") !== -1) 
     { 
      alert('link found'); 
      current.click(); 
     } 

    } 
} 
+0

id在網站上是一樣的。這是主要困難。 – Braheen 2013-05-14 16:31:00

+0

很好地觀察到,但是當選擇發生在tagNames上時,這可能不是問題,並且應該在我看來是一個評論! – adeneo 2013-05-14 16:31:16

+1

是不是你的網站?然後找到一種方法來改變他們,你會沒事的。 @adeneo我正在更新答案,你是對的! – 2013-05-14 16:34:09