I want to generate a link that is clicked right after creating, but nothing happens
Code:
var link = $("<a></a>");
link.attr("href", "/dostuff.php");
link.attr("target", "_blank");
link.click();
The attributes are set correctly:
var link = $("<a></a>");
link.attr("href", "/dostuff.php");
link.attr("target", "_blank");
var linkcheck = link.wrap('<p>').parent().html();
console.log(linkcheck);
This returns:
<a href="/dostuff.php" target="_blank"></a>
No errors
UPDATE
I tried to append it, bind click to it, click it and remove it
var link = $("<a></a>");
link.attr(
{
id : "linky",
href : "/dostuff.php",
target: "_blank"
});
$("body").append(link);
$("#linky").on("click", function() { console.log("Link clicked"); });
$("#linky").click();
$("#linky").remove();
The click action is being executed, but the default action (open the link) isn't..
UPDATE2
I have found the solution: creating and submitting a <form>
! See my answer.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…