Soundcloud API Javascript错误:$未定义
Soundcloud API Javascript Error : $ is not defined
我是一个javascript初学者,当我试图使用soundcloud
API时,我得到了这个错误:
ReferenceError: $ is not defined
$("#stream").live("click", function(){
我删除了client_ID和track_ID,但这是我的脚本:
<script type="text/javascript" src="http://connect.soundcloud.com/sdk.js"></script>
<script type="text/javascript">
SC.initialize({
client_id: "000000000000000000"
});
$("#stream").live("click", function(){
SC.stream("/tracks/000000000", {autoPlay: true});
});
</script>
和HTML:
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/player.css" />
</head>
<body>
<div id="container">
<h1>earth. home. destroyed.</h1>
<nav>
<ul>
<li id="stream">► saturn boy</li>
<li>► gold</li>
<li>► earth</li>
<li>► from the sun</li>
<li>► cosmic landscapes</li>
</ul>
</nav>
</div>
</body>
任何帮助都将是非常感激的:)
谢谢!!
从脚本的示例中,看起来您缺少对jQuery的引用(这不是默认库-您必须在需要它的页面上使用它的任何脚本之前显式包含它)。
添加这个(在其他脚本标签之前)应该可以修复它:
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
另外,我建议不要使用'live',因为它已经被弃用了。相反,你应该使用"on"answers"delegation"。对于上面的HTML示例,可以使用以下编辑来完成:
$('#container > h1 > nav > ul').on('click', '#stream', function(){
SC.stream("/tracks/000000000", {autoPlay: true});
});
- 这将触发处理程序,只有当点击是在
#stream
元素-但#stream
元素不需要在页面上开始-只有容器ul
。如果你也在动态创建ul
,你可以"上升"一级并将选择器更改为#container > h1 > nav
(这有类似的效果,但这里的ul
也可以动态添加)。
关于'on'事件委托的更多信息:https://api.jquery.com/on/
. .希望这对你有帮助!
相关文章:
- chrome.storage.sync未定义错误
- 对象为null或IE9中存在未定义错误
- 已定义变量出现未定义错误
- 'define' 在 RequireJS 和 Webapp Yo 生成器上未定义错误
- onreadystatechange未定义错误
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 如何修复JSlint报告的“foo未定义”错误
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 主干无法读取属性'属性'主干视图中的未定义错误
- 如何克服javascript中的未定义错误
- require未定义错误
- 单击链接时出现函数未定义错误
- 函数为未定义错误
- 主干未定义错误
- 使用Javascript打印HTML:未定义错误
- 避免“无法读取”属性'获取'的未定义错误
- 未捕获类型错误的coffescription:无法读取属性'offsetWidth'的未定义错误
- ajax上get函数内部的未定义错误
- Javascript变量未定义错误
- AngularJS指令作用域未解析(“属性名称未定义”错误)