简体   繁体   English

Javascript有效,但JQuery无效吗?

[英]Javascript works but JQuery does not?

My code is like this: 我的代码是这样的:

<script> 
    var name = document.getElementById("nusname").innerHTML;
    document.getElementById("uName").innerHTML = "Welcome, " + name;
    $('#uName').text("AsgufHFBS");
</script>

The 3rd line 第三行

document.getElementById("uName").innerHTML = "Welcome, " + name;

works but the 4th line 可以,但是第四行

$('#uName').text("AsgufHFBS");

does not. 才不是。

I have tried replacing this with 我试图用替换

$(document).ready(function(){
    $('#uName').text("AsgufHFBS");
});

but it still does not work. 但它仍然不起作用。

So my question is this: Why does the javascript work but the not the JQuery version? 所以我的问题是:为什么javascript可以工作,而不是JQuery版本?

Add this in the <head> of your document: 将其添加到文档的<head>中:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

Or simply update your code: 或者只是更新您的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script> 
    var name = document.getElementById("nusname").innerHTML;
    document.getElementById("uName").innerHTML = "Welcome, " + name;
    $('#uName').text("AsgufHFBS");
</script>

Assuming that you have already embedded the jQuery library correctly and it's still not working, try this: 假设您已经正确嵌入了jQuery库,但仍无法正常工作,请尝试以下操作:

jQuery('#uName').text("AsgufHFBS");

If that works it means the $ shorthand is likely reserved by another script on your page. 如果这样可行,则意味着$速记可能已由页面上的另一个脚本保留。 You can either correct that issue, or just preface jQuery commands with "jQuery" rather than "$" on that page. 您可以纠正该问题,也可以在该页面上使用“ jQuery”而不是“ $”作为jQuery命令的开头。

I have coded your code into jsFiddle and it works fine, however did you include jQuery in your file? 我已经将您的代码编码到jsFiddle中,并且工作正常,但是您是否在文件中包含jQuery? If you press f12 in your browser do you get any console logs? 如果在浏览器中按f12键,会得到任何控制台日志吗? If so please provide them. 如果是这样,请提供它们。

But here is the jsFiddle version which does include jQuery 但是这是包含jQuery的jsFiddle版本

jsFiddle : https://jsfiddle.net/bn788ff4/ jsFiddle: https ://jsfiddle.net/bn788ff4/

Html HTML

<div id="nusname">hello</div>
<div id="uName">username</div>

Javascript / jQuery Javascript / jQuery

var name = document.getElementById("nusname").innerHTML;
document.getElementById("uName").innerHTML = "Welcome, " + name;
$('#uName').text("AsgufHFBS");

To include jQuery you can use the hosted version by google 要包含jQuery,您可以使用Google托管的版本

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

Link https://developers.google.com/speed/libraries/ 链接https://developers.google.com/speed/libraries/

You are probably missing Jquery library files. 您可能缺少Jquery库文件。 Please attach the foolowing to the <head> section of your page. 请将附加信息附加到页面的<head>部分。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

and replace the 4th line of your code with: 并将代码的第四行替换为:

$('#uName').html("AsgufHFBS"); 

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

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