【问题标题】:Linking to jquery and javascript is not working链接到 jquery 和 javascript 不起作用
【发布时间】:2014-01-31 20:49:53
【问题描述】:

在 Coda 2 中,我可以链接到 css,但是当我链接到很多东西时,它不起作用,因为没有任何反应,尤其是使用 jquery。这有什么问题? Btw here it is so far in jsbin if it matters

main.html

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="planets.css">
<script src="planets.js"></script>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>

<title>Planets</title>
</head>

planets.js:

$('#submit').click(function () {
    var checkedPlanet = $("input[name='planets']:checked").val(); 
  $("input:radio").attr("checked", false);

  var age = document.getElementById('age').value;
  var newAge;
  if(checkedPlanet==='mercury'){
    newAge = Math.round(age/0.241);
    alert("You would be"+" "+newAge+" "+ "years old on Mercury!");
  }
  if(checkedPlanet==='venus'){
    newAge = Math.round(age/0.615);
    alert("You would be"+" "+newAge+" "+ "years old on Venus!");
  }
  if(checkedPlanet==='earth'){
    alert('You would be'+" "+ age*1+" " +'on Earth! Duh!');
  }
  if(checkedPlanet==='mars'){
    newAge=Math.round(age/1.88);
    alert("You would be"+" "+newAge+" "+ "years old on Mars!");
  }
  if(checkedPlanet==='jupiter'){}

});

【问题讨论】:

  • 定义“它不起作用”。不行怎么办?此外,您发布的 jsbin 链接与您在此处发布的代码有很大不同; linkscript 标签的数量不同。什么不起作用?

标签: javascript jquery coda


【解决方案1】:

Quentin 用代码示例说的:

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script> <!-- Lose http:, and jQuery will load over https too -->
    <script src="planets.js"></script>

    //Wait until the DOM is ready
    $(function(){
        //your code
    });

【讨论】:

  • 谢谢,但这仍然不起作用:
【解决方案2】:

您正在尝试在 planets.js 中使用 jQuery,然后您正在加载 jQuery。

交换两个脚本元素。


此外,您在planets.js 中尝试做的第一件事是将点击事件处理程序绑定到您共享的代码中不存在的ID为submit的东西。

推测它存在于文档的更下方,但在 JS 运行时它不会被浏览器解析。

将脚本移动到要绑定的元素之后,或将其中的代码包装在一个函数中,作为 DOM 就绪事件处理程序传递。

【讨论】:

  • 它仍然没有做任何事情我 SOOOO 沮丧 &lt;script src="http://code.jquery.com/jquery-2.0.3.js"&gt;&lt;/script&gt; &lt;script src="planets.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="planets.css"&gt;跨度>
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多