2013-05-14 40 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