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>

所有链接上的 ID 名称都相同。这是主要的困难。

我想点击第二个链接我的 javascript 代码是配置网络浏览器是

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();
        }

    }
}

我该怎么做才能匹配条件?

4

1 回答 1

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 参考

更新了 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();
        }

    }
}
于 2013-05-14T16:28:50.767 回答