[英]How to include a link with onclick in an alert that's not always visible in JavaScript?
I have an alert that shows under some conditions. 我有在某些情况下显示的警报。 I want the alert to contain a link and execute some code when that link is clicked.
我希望警报包含一个链接,并在单击该链接时执行一些代码。 However, I'm getting error "ReferenceError: doTheThing is not defined".
但是,我收到错误“ ReferenceError:doTheThing未定义”。
function doTheThing() {
document.getElementById("thing").innerHTML = "Done.";
};
var alert = '<div class="alert alert-warning alert-dismissible fade show"
role="alert"><button type="button" class="close" data-dismiss="alert"
aria-label="Close"><span aria-hidden="true">×</span></button>Please
submit your wallet address or <span class="btn-link"
id=btn-donate onclick="doTheThing()">click here to donate your winnings.</span></div>';
var alertHTML = $(alert);
document.getElementById("placeholder").innerHTML = alert;
and html; 和html;
<div id=placeholder></div>
<div id=thing></div>
I assume: you defined the function in the dom ready context. 我假设:您在dom准备就绪上下文中定义了该函数。 If so, move the function outside.
如果是这样,请将功能移到外面。
function doTheThing() { document.getElementById("thing").innerHTML = "Done."; }; $(function () { var alert = '<div class="alert alert-warning alert-dismissible fade show"\\ role="alert"><button type="button" class="close" data-dismiss="alert"\\ aria-label="Close"><span aria-hidden="true">×</span></button>Please\\ submit your wallet address or <span class="btn-link"\\ id=btn-donate onclick="doTheThing()">click here to donate your winnings.</span></div>'; var alertHTML = $(alert); document.getElementById("placeholder").innerHTML = alert; });
.row { background: #f8f9fa; margin-top: 20px; } .col { border: solid 1px #6c757d; padding: 10px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" > <div id=placeholder></div> <div id=thing></div>
This code works fine: 这段代码可以正常工作:
function doTheThing() { document.getElementById("thing").innerHTML = "Done."; }; var alert = `<div class="alert alert-warning alert-dismissible fade show" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Please submit your wallet address or <span class="btn-link" id=btn-donate onclick="doTheThing()">click here to donate your winnings.</span></div>`; var alertHTML = $(alert); document.getElementById("placeholder").innerHTML = alert;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='placeholder'> </div> <div id='thing'> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.