簡體   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