有人可以修复我的jquery代码吗? [英] Can someone fix my jquery code?

查看:88
本文介绍了有人可以修复我的jquery代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坐着写这段代码大约4个小时我仍然无法正常工作

有人可以帮我吗?

这是HTML代码:

<!DOCTYPE html> 
< html>
< head>
< link rel ='stylesheet'type ='text / css'href ='style.css'>
< / head>
< body>
< button id =button> test< / button>
< h1 style =font-size:1.5em; font-family:verdana; color:black;>答案< / h1>
< script src ='https://code.jquery.com/jquery-3.1.0.min.js'>< / script>
< script type =text / javascript>
$('#button')。on('click',function(){
var randomAnswer = Math.random();
if(randomAnswer< 0.1){
alert(hey);
} else if(randomAnswer< = 0.2){
alert(hey);
} else if(randomAnswer< = 0.3){
alert(hey);
}否则if(randomAnswer< = 0.4){
alert(hey);
} else if(randomAnswer< = 0.5) {
alert(hey);
} else if(randomAnswer< = 0.6){
alert(hey);
} else if(randomAnswer< = 0.7){
alert(hey);
} else if(randomAnswer< = 0.8){
alert(hey);
} else if(randomAnswer< ; = 0.9){
alert(hey);
} else {
alert(hey);
}
});
$(document).ready(main);
< / script>
< / body>
< / html>



我添加了警报(嘿);只是为了看看它是否有效,我不知道为什么,但jquery不与html交互。



我尝试过:



我去了很多指南和教程以及w3schools但仍然没有找到解决方法

解决方案

('#button')。on('click',function(){
var randomAnswer = Math.random();
if(randomAnswer< 0.1){
alert(hey);
}否则if(randomAnswer< = 0.2){
alert(hey);
}否则if(randomAnswer< = 0.3) {
alert(hey);
} else if(randomAnswer< = 0.4){
alert(hey);
} else if(randomAnswer< = 0.5){
alert(hey);
}否则if(randomAnswer< = 0.6){
alert(hey);
} else if(randomAnswer< ; = 0.7){
alert(hey);
}否则if(randomAnswer< = 0.8){
alert(hey);
}否则if( randomAnswer< = 0.9){
alert(hey);
} else {
alert(hey);
}
});
(文件)。就绪(主);
< / script>
< / body>
< / html>



我添加了警报(嘿);只是为了看看它是否有效,我不知道为什么,但jquery不与html交互。



我尝试过:



我去了很多指南和教程以及w3schools但仍然没有找到解决方法


它归结为jQuery的基础,即选择器。您是否理解以下关系:

< button id =button> 

 


I've been sitting and writing this code for about 4 hours and I still can't get it work
can someone help me please?
this is the html code:

<!DOCTYPE html>
<html>
<head>
  <link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<button id = "button">test</button>
<h1 style = "font-size: 1.5em; font-family: verdana; color: black;">the answer</h1>
<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>
<script type="text/javascript">
$('#button').on('click', function() {
	var randomAnswer = Math.random();
	if(randomAnswer < 0.1) {
alert("hey");
	} else if(randomAnswer <= 0.2) {
alert("hey");
	} else if(randomAnswer <= 0.3) {
alert("hey");
	} else if(randomAnswer <= 0.4) {
alert("hey");
	} else if(randomAnswer <= 0.5) {
alert("hey");
	} else if(randomAnswer <= 0.6) {
alert("hey");
	} else if(randomAnswer <= 0.7) {
alert("hey");
	} else if(randomAnswer <= 0.8) {
alert("hey");
	} else if(randomAnswer <= 0.9) {
alert("hey");
	} else {
alert("hey");
	}
});
$(document).ready(main);
</script>
</body>
</html>


I added the alert("hey"); just to see if it works and I don't know why but the jquery doesn't interact with the html.

What I have tried:

I went to a lot of guides and tutorial and to w3schools and still didn't find how to fix it

解决方案

('#button').on('click', function() { var randomAnswer = Math.random(); if(randomAnswer < 0.1) { alert("hey"); } else if(randomAnswer <= 0.2) { alert("hey"); } else if(randomAnswer <= 0.3) { alert("hey"); } else if(randomAnswer <= 0.4) { alert("hey"); } else if(randomAnswer <= 0.5) { alert("hey"); } else if(randomAnswer <= 0.6) { alert("hey"); } else if(randomAnswer <= 0.7) { alert("hey"); } else if(randomAnswer <= 0.8) { alert("hey"); } else if(randomAnswer <= 0.9) { alert("hey"); } else { alert("hey"); } });


(document).ready(main); </script> </body> </html>


I added the alert("hey"); just to see if it works and I don't know why but the jquery doesn't interact with the html.

What I have tried:

I went to a lot of guides and tutorial and to w3schools and still didn't find how to fix it


It boils down to the very basic of jQuery, the Selectors. Do you understand the relation between:

<button id = "button">

and


这篇关于有人可以修复我的jquery代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆