锚标记onclick无效

时间:2016-06-28 14:36:20

标签: javascript anchor

我使用javascript在列表中创建锚标签,但是onClick没有触发。我知道onClick正在调用的函数,因为我用一个临时提交按钮来测试它,但我需要锚标记来调用它。这是我的代码。

function getTablePages(div,pages,index){
index -= 1;
var mydiv;
switch (div){
    case 'openSales': mydiv = document.getElementById("openSalesDiv"); break;
    case 'leadsSales': mydiv = document.getElementById("leadsSalesDiv"); break;
    case 'overdueTickets': mydiv = document.getElementById("overdueTicketsDiv"); break;
    case 'readyToBill': mydiv = document.getElementById("readyToBillDiv"); break;
    case 'overdueSales': mydiv = document.getElementById("overdueSalesDiv"); break;
    case 'overdueProjects': mydiv = document.getElementById("overdueProjectsDiv"); break;
    case 'wfcTickets': mydiv = document.getElementById("wfcTicketsDiv"); break;
    case 'openTickets': mydiv = document.getElementById("openTicketsDiv"); break;
    case 'assignedPM':  mydiv = document.getElementById("assignedPMDiv");
}
var ulTag = document.createElement('ul');
ulTag.className = 'Pagination';
ulTag.style = 'display:block;';
var liTag = document.createElement('li');
var aTag = document.createElement('a');
aTag.setAttribute('href',"#");
aTag.innerHTML = "<";
liTag.appendChild(aTag);
ulTag.appendChild(liTag);
for (v = 0; v < pages; v++){
    if(v+6 >= index && v < index+5){
        var liTag = document.createElement('li');
        var aTag = document.createElement('a');
        aTag.setAttribute('href',"#");
        switch (div){
            case 'openSales': aTag.onClick = "getOpenSalesClick(this.innerHTML)"; break;
            case 'leadsSales': aTag.onClick = "getleadsSalesClick(this.innerHTML)"; break;
            case 'overdueTickets': aTag.onClick = "getOverdueTicketsClick(this.innerHTML)"; break;
            case 'readyToBill': aTag.onClick = "getReadyToBillProjectsClick(this.innerHTML)"; break;
            case 'overdueSales': aTag.onClick = "getOverdueSalesClick(this.innerHTML)"; break;
            case 'overdueProjects': aTag.onClick = "getOverdueProjectsClick(this.innerHTML)"; break;
            case 'wfcTickets': aTag.onClick = "getWFCTicketsClick(this.innerHTML)"; break;
            case 'openTickets': aTag.onClick = "getOpenTicketsClick(this.innerHTML)"; break;
            case 'assignedPM':  aTag.onClick = "getAssignedPMClick(this.innerHTML)";
        }
        aTag.innerHTML = (v+1);
        liTag.appendChild(aTag);
        ulTag.appendChild(liTag);
    }
}
    var aTag = document.createElement('a');
aTag.setAttribute('href',"#");
aTag.innerHTML = ">";
liTag.appendChild(aTag);
ulTag.appendChild(liTag);
mydiv.appendChild(ulTag);
}

function getOpenSalesClick(x){
document.getElementById('openSalesDiv').innerHTML = '';
}

<input type='submit' onclick="getOpenSalesClick()" value="Delete"></input>

1 个答案:

答案 0 :(得分:0)

事件处理程序在JavaScript中往往是小写的。请尝试onclick而不是onClickonclick处理程序还需要引用函数而不是字符串,并且您可能希望在每次单击时调用函数getOpenSalesClick而不是getOpenSalesClick()的结果:

case 'openSales': aTag.onclick = getOpenSalesClick;