【问题标题】:uncaught refference error: alertbox with value from a list未捕获的参考错误:带有列表值的警报框
【发布时间】:2016-03-08 20:57:37
【问题描述】:

如果单击显示列表中第一个值的按钮,我会尝试获取警报。我正在使用基于此threat 的以下代码。在这种威胁中,他们使用的是表格而不是列表。但我认为那不是问题……我认为是现在。我使用的代码是这样的。(这些特征来自一个开放层地图):

<ul id ="table_overvieuw">
<#list features as feature>
  <li><b>Type: ${type.name}</b> (id: <em>${feature.fid}</em>):
  <ul>
  <#list feature.attributes as attribute>
    <#if !attribute.isGeometry>
      <li><b>${attribute.name}</b></li>
      ${attribute.value}
    </#if>
  </#list>
  <br><br>
 <input type="button" name="geef slijpplaten info" class="ok" value="OK" onclick="myFunction()" />
  </ul>
  </li>
</#list>
</ul>
<script type="text/javascript">
function highlight(e) {
    if (selected[0]) selected[0].className = '';
    e.target.parentNode.className = 'selected';
}

var table = document.getElementById('table_overvieuw'),
    selected = table.getElementsByClassName('selected');
table.onclick = highlight;

function myFunction(){

    alert($("li.selected li:first" ).html());
}

</script>

我在控制台中遇到的错误是:Uncaught ReferenceError: $ is not defined。 $ 来自这段代码:

 alert($("li.selected li:first" ).html());

有人可以帮帮我吗?

【问题讨论】:

  • $('...') 是 jquery 选择器,你的 html 页面中包含 jquery 吗?
  • 谢谢!我没有意识到我正在使用它

标签: javascript jquery html


【解决方案1】:

要使用 jquery,您必须将它放在您的页面上。 使用 CDN (http://jquery.com/download/#using-jquery-with-a-cdn)。 您也可以使用 bower (bower install jquery) 安装它并在页面上链接它:

<html>
<head>
    <script src="/bower_components/jquery/dist/jquery.min.js"></script>
...
</head>
...
</html>

【讨论】:

  • 或者你可以使用
【解决方案2】:

如果列表框的格式正确,请在警告框中尝试以下代码:-

$('ul#table_overview li:first').text()

【讨论】:

  • 我需要把它准确地放在哪里?
  • function myFunction(){ alert($("li.selected li:first" ).html()); } 用上面的代码替换已有的代码
  • 你能解释一下它的作用吗?因为我填写了它(带有 $ 的警报)并且警报有一个没有任何内容的响应
猜你喜欢
  • 1970-01-01
  • 2017-08-31
  • 2016-10-03
  • 2020-08-31
  • 1970-01-01
  • 2019-01-20
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多