简体   繁体   English

当我添加“”时,我尝试与我的 javascript 链接的按钮 onlick 似乎不起作用或在我的 vs 代码上突出显示橙色

[英]button onlick that i tried linking with my javascript doesn't seem to be working or highlighting orange on my vs code when i add the “”

i have tried everything in order to fix the issue and even looked at the video of the tutorial im learning from but can't seem to get it working crrectly.我已经尝试了一切以解决问题,甚至查看了我正在学习的教程视频,但似乎无法正常工作。 Even checked around the internet.甚至在互联网上进行了检查。 i added the javascript code so you guys can see that i did define it.我添加了 javascript 代码,所以你们可以看到我确实定义了它。 im new to all this so explaining it best you can would be greatly appreciated我对这一切都是新手,所以最好地解释一下,您将不胜感激

  <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></link>
        <link rel="stylesheet" href="style.css">
        <title>Javascript Your Age In Days</title>
    </head>
    <body>
        <div class="container-1">
            <h2>Challenge 1: Your Age In Days</h2>
                <div class="flex-box-container-1">
                
            
                <div>
                    <button class="btn btn-primary" onclick='ageInDays()'>Click Here</button>
                </div>
            
                <div>
                    <button class="btn btn-danger">Reset</button>
                </div>
            
                <div class="flex-box-container-1">
                    <div id="flex-box-container-1"></div>
                </div>
                
                </div>
    
            </div>
        
        
        
         <script src="script.js"></script>
    
    </body>
    </html>
    
    
  
function ageInDays() {
    let birthYear = prompt("what year were you born...Good friend?");

}

      
        
       

It not working because you are using in line script for the invocation of the ageInDays function, but the definition is in another file that has not loaded when the browser read the inline JS so there are two ways of fixing this.它不起作用,因为您正在使用内联脚本来调用ageInDays函数,但定义位于另一个文件中,当浏览器读取内联 JS 时,该文件尚未加载,因此有两种方法可以解决此问题。

  1. Use an inline script so the JS is already on the browser same time as HTML like使用内联脚本,这样 JS 就已经与 HTML 一样在浏览器上了
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></link>
        <link rel="stylesheet" href="style.css">
        <title>Javascript Your Age In Days</title>
    </head>
    <body>
        <div class="container-1">
            <h2>Challenge 1: Your Age In Days</h2>
                <div class="flex-box-container-1">
                <div>
                    <button class="btn btn-primary" onclick="ageInDays()">Click Here</button>
                </div>
                <div>
                    <button class="btn btn-danger">Reset</button>
                </div>
                <div class="flex-box-container-1">
                    <div id="flex-box-container-1"></div>
                </div>
                </div>
            </div>
         <script>
             function ageInDays() { let birthYear = prompt("what year were you born...Good friend?"); } 
         </script>
    </body>
    </html>
  1. handle everything in the script file处理脚本文件中的所有内容
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></link>
        <link rel="stylesheet" href="style.css">
        <title>Javascript Your Age In Days</title>
    </head>
    <body>
        <div class="container-1">
            <h2>Challenge 1: Your Age In Days</h2>
                <div class="flex-box-container-1">
                <div>
                    <button class="btn btn-primary" id="action">Click Here</button>
                </div>
                <div>
                    <button class="btn btn-danger">Reset</button>
                </div>
                <div class="flex-box-container-1">
                    <div id="flex-box-container-1"></div>
                </div>
                </div>
            </div>
         <script src="main.js"></script>
    </body>
    </html>

and in the main.js并在 main.js

const button = document.getElementById('action');
button.addEventListener('click', function(){
    let birthYear = prompt("what year were you born...Good friend?"); 
});

Add this to your script.js file, and remove onclick='ageInDays()' from your button.将此添加到您的script.js文件,并从您的按钮中删除onclick='ageInDays()'

let btn = document.querySelector(".btn-primary");
btn.addEventListener("click", ageInDays);

function ageInDays(){
  //code to be executed when you clicked the button
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经尝试过,但是我的程序似乎不起作用 - I have tried but my program doesn't seem to work 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我似乎无法让我的悬停效果在按钮上工作。 尝试了很多技巧 - i can't seem to get my hover effects to work on button. have tried many tips 我的 Javascript 天气应用程序中的搜索按钮似乎不起作用 - Search Button in my Javascript Weather Application doesn't seem to work 我试图将我的标签附加到我的li标签上,但它似乎不起作用? - I'm trying to append my a tag to my li tag but it doesn't seem to be working? 我的 Javascript 似乎不起作用 - DOM 是否已更改? - My Javascript doesn't seem to be working - could the DOM have changed? 我在 Javascript 中的 HTML 表单验证器似乎无法正常工作 - My HTML form validator in Javascript doesn't seem to be working 我的正则表达式javascript验证似乎不起作用 - My regex javascript validation doesn't seem to be working 我不知道为什么当我在我的 HTML 程序上单击“Click = Past”按钮时,绿色字符图像不显示我在可汗学院尝试过 - I don't know why When I click the button "Click = Past" on my HTML program the green character image doesn't show up I tried to this in Khan academy 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM