gpt4 book ai didi

javascript - 单击按钮时调用脚本

转载 作者:行者123 更新时间:2023-12-03 06:53:38 26 4
gpt4 key购买 nike

我有一个有助于注释图像的脚本。我只想在单击按钮时运行此脚本。我尝试使用按钮的 onclick 事件,但它不起作用。这是代码

<head>
<script src="javascripts/jquery.js"></script>
<script src="javascripts/jquery-ui.min.js"></script>
<link rel="stylesheet" href="stylesheets/jquery-ui.css">

////////////// those are the scripts that i use in the function //////
<link rel="stylesheet" href="highlight.js/zenburn.css">
<link rel="stylesheet" href="stylesheets/css/theme-dark/annotorious-dark.css" />
<script src="highlight.js/highlight.pack.js"></script>
<script src="javascripts/annotorious.min.js"></script>
/////////////
<script>
$("#annotation").click(function() {
hljs.initHighlightingOnLoad();
//function of annotation
});
</script>
</head>
<body>
<button type="submit" id="annotation" >
<img src="stylo.png" style="width: 30px ; height:50px" alt="Submit">
</button>
</body>

但这不起作用,当我加载页面时脚本仍然在运行。我只需要在单击“注释”按钮时调用它。

最佳答案

给你

<script>
$(document).ready(function(){
$("#annotation").click( function()
{
alert("Hello!");
// hljs.initHighlightingOnLoad(); //function of annotation
});
});
</script>

<button type="submit" id="annotation" > <img src="stylo.png" style="width: 30px ; height:50px" alt="Submit"></button>

我不知道你的 hljs.initHighlightingOnLoad();函数,但就按钮单击时的函数执行而言,您必须在 $(document).ready() 函数中编写该代码。它工作得很好,因为我已经使用alert()进行了测试;

<小时/>

----------------------------------------已更新------------ --------------------

我使用jquery和highlightjs的cdn进行了测试

<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.9.1/highlight.min.js"></script>

<script>
$(document).ready(function(){
$("#annotation").click( function()
{
hljs.initHighlightingOnLoad(); //function of annotation
});
});
</script>
</head>
<body>

<button type="submit" id="annotation" >
<img src="stylo.png" style="width: 30px ; height:50px" alt="Submit">
</button>
</body>

关于javascript - 单击按钮时调用脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37387091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com