简体   繁体   English

如何在JavaScript中并不总是可见的警报中包含带有onclick的链接?

[英]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">&times;</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">&times;</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> 

In your provided example change following settings: 在您提供的示例中,更改以下设置:

在此处输入图片说明

As Renan Souza sad, it has to be included inside its own file. Renan Souza感到悲伤的是,它必须包含在自己的文件中。

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">&times;</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.

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