javascript按钮没有在IE中显示

时间:2014-01-06 17:21:28

标签: javascript internet-explorer button mozilla

以下代码在表格行的一端创建一个蓝色按钮,该按钮链接到您所在的客户端的详细信息页面(您所在的行)。此代码适用于Firefox,但在Internet Explorer中没有按钮显示,因此您无法访问详细信息页面。有人可以建议一个可以在IE和Firefox中使用的解决方案吗?

$('#account-table tbody tr').each( function () {
        //nCloneTd.innerHTML = '<a href="../two/'+this.id+'"><button class="btn   btn-mini btn-primary" type="button">Detail</button></a>';
        nCloneTd.innerHTML = '<a href="'+this.id+'"><button class="btn btn-mini   btn-primary" type="button">Detail</button></a>';
        nCloneTd.id = "detail_cell";
        nCloneTd.className = "center";
        nCloneTd.bgColor = "white"
        this.insertBefore(  nCloneTd.cloneNode( true ),   this.childNodes[this.childNodes.length] );
      });

其中一个答案询问开发工具控制台对页面/错误的评价。在查看IE中的开发工具控制台后,我发现问题在于这条线......

$('#account-table thead tr').each( function () {
            this.insertBefore( nCloneTh, this.childNodes[this.childNodes.length] );
            });

改变......

this.insertBefore( nCloneTh, this.childNodes[this.childNodes.length] );

是...

this.insertBefore( nCloneTh);

和......

this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[this.childNodes.length]);

是...

this.insertBefore(  nCloneTd.cloneNode( true ));

使它在IE中正常工作并在Firefox中失败(“Node.InsertBefore的参数不够”)。所以问题是,是否有条件我可以在javascript中放置这两个语句,以便它为IE做一件事而对Mozilla做另一件事?

1 个答案:

答案 0 :(得分:0)

您的jquery循环和javascript插入可以重构。

$("#account-table tbody tr").each(function () { 
    var $btn = $("<a>", 
    {
        href: this.id,
        html: "Detail",
        "class": "btn btn-mini btn-primary center",
        id: "detail_cell_" + this.id,
        style: "background-color: #fff"
    });

    $(this).append($btn);
});

您将n<a>个新代码标记为id,这不是一个好主意。

您似乎正在使用自举按钮,而不是在<button>标记内部<a>,您可以为<a>标记提供btn