繁体   English   中英

锚标记在javascript中不起作用

[英]Anchor tag is not working in javascript

我使用document.write()来打印锚标记,但它不能在这里工作是我的代码,所以任何人都可以帮助我解决我错的地方。 谢谢

var name = 'addtowish<?php echo $row->fields("id");?>';
var value = '<?php echo $row->fields("id");?>';    
document.write(
    "<a href='#' onclick='"+ createCookie(name, value, '15') +"'>" +
        "add to wishlist" +
    "</a>"
);

您应该将createCookie()函数作为字符串传递

document.write(
    "<a href='#' onclick=\"createCookie('"+ name +"','" + value +"', '15')\">" +
        "add to wishlist" +
    "</a>"
);

如果您正在使用强大而纯粹的JS解决方案,那么这样的事情应该可以解决问题,但这取决于您的最终游戏是什么:

var anchor      = document.createElement("a");
var anchorText  = document.createTextNode("add to wishlist");
anchor.appendChild(anchorText);
anchor.href     = "#";
anchor.id       = "addLink";

document.write(anchor);

window.onload = function() {
    var name = "addtowish";
    var value = 99;
    document.getElementById("addLink").onclick = function(e) {
        createCookie(name, value, 15);    
        e.preventDefault();
        return false;
    };
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM