简体   繁体   English

JQuery 无法识别 #id 选择器,似乎依赖于 URL

[英]JQuery isn't recognising a #id selector, seems to be dependent on the URL

I'm writing a Ruby on Rails app.我正在写一个 Ruby on Rails 应用程序。 The following jQuery code is included in the head tag of the index.html.erb file, which is the template for all pages on the site.以下 jQuery 代码包含在 index.html.erb 文件的 head 标签中,该文件是站点上所有页面的模板。

<script>
  $(document).ready(function() {
    $("#select_mailshot").click(function () {
      alert('mailshot');
      document.location.href = "/products/1";
    });
    $("#select_blog").click(function () {
      alert('blog');
      document.location.href = "/messages";
    });
    $("#select_contact").click(function () {
      alert('contact');
      document.location.href = "/contacts/1";
    });
  });
</script>

(the alert steps are in there for debugging) (警报步骤在那里用于调试)

The following html code in index.html.erb以下 html 代码在 index.html.erb

<ul>
  <li id="select_mailshot">Mailshot</li>
  <li id="select_blog">Blog</li>
  <li id="select_contact">Contact us</li>
</ul>

The intention is that this effectively creates 3 buttons.这样做的目的是有效地创建 3 个按钮。

When clicking on any button from http://myurl.com/ it all works.单击http://myurl.com/中的任何按钮时,一切正常。

When clicking on any button from http://myurl.com/messages (get to this via the middle button) it all works单击http://myurl.com/messages中的任何按钮时(通过中间按钮访问此按钮),一切正常

When starting from http://myurl.com/products/1 it all stops working (the alerts do not trigger).http://myurl.com/products/1开始时,它都停止工作(警报不会触发)。 In fact when starting from http://myurl.com/anything/id it stops working.事实上,当从http://myurl.com/anything/id开始时,它会停止工作。

I've been trying to solve this for hours now and the only difference between the working and non-working conditions is the url as far as I can see.我已经尝试解决这个问题几个小时了,据我所知,工作条件和非工作条件之间的唯一区别是 url。

Can anyone shed any light as to what's going on here?任何人都可以解释这里发生了什么吗?

What does firebug tell you?萤火虫告诉你什么? Do you have javascript errors on the page?页面上是否有 javascript 错误? if so, what are they?如果有,它们是什么? Are you sure the jQuery library is included correctly in the deeper pages ( ie is it a relative path? )您确定 jQuery 库正确包含在较深的页面中(即它是相对路径吗?)

Is this javascript inlined?这是 javascript 内联吗?

If not, then maybe the link is relative so when you try to load it from messages/40 you need ../script.js .如果不是,那么链接可能是相对的,因此当您尝试从 messages/40 加载它时,您需要../script.js Another solution is to use absolute URLs ( http://myurl/script.js ) or virtual (/script.js).另一种解决方案是使用绝对 URL ( http://myurl/script.js ) 或虚拟 (/script.js)。

I was using FaceBox to create modal overlays and I couldn't figure out why I was having the same problem.我正在使用 FaceBox 创建模态叠加层,但我不知道为什么会遇到同样的问题。

I turns out that the listener wasn't being attached to HTML elements until it was visible.我发现在它可见之前,监听器并没有被附加到 HTML 元素上。 (The items were available if I viewed source, but jQuery seemed not to attach a listener until it was visible.) (如果我查看源代码,这些项目是可用的,但 jQuery 似乎在它可见之前没有附加监听器。)

For anyone having the same problem, I'd suggest moving your click() code to a point where the HTML element you're attaching to is visible.对于遇到相同问题的任何人,我建议将您的 click() 代码移动到您要附加的 HTML 元素可见的位置。

Also, I've found selecting by ID does give more problems than class.此外,我发现按 ID 选择确实比 class 带来更多问题。 I have no idea why.我不知道为什么。 (No, there were not duplicate IDs.) (不,没有重复的 ID。)

Hope this helps someone with the same problem!希望这可以帮助有同样问题的人!

Thanks to cherouvim and digitaljoel.感谢 cherouvim 和 digitaljoel。

This was due to javascript files being included relative to the current URL.这是因为 javascript 文件相对于当前的 URL 包含在内。

The following was included in the head tag以下内容包含在 head 标签中

<script type="text/javascript" src="javascripts/jquery-1.3.2.js"></script>

I changed it to我把它改成

<script type="text/javascript" src="/javascripts/jquery-1.3.2.js"></script>

(note the extra "/" in the src attribute) and everything works as expected. (注意 src 属性中额外的“/”),一切都按预期工作。

I worked this out after checking the error logs on the server and in the browser.我在检查服务器和浏览器上的错误日志后解决了这个问题。

Feeling a little dumb but a lesson well learned.感觉有点笨,但吸取了教训。

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

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