简体   繁体   English

OnClick获得“ a”标签的“ id”属性

[英]OnClick get “id” attribute of “a” tag

I have list view in jQuery Mobile web app this list view is made of elements like this: 我在jQuery Mobile Web应用程序中具有列表视图,此列表视图由以下元素组成:

<li id='search_r'>
    <a href='#' id='$id' class='s_result'></a>
</li>
<li id='search_r'>
    <a href='#' id='$id' class='s_result'></a>
</li>

Number of element depends on number of search results, its not only these two. 元素的数量取决于搜索结果的数量,不仅限于这两个。 When I click on element in list view, in this case: 在这种情况下,当我单击列表视图中的元素时:

<li></li>

I need 2 things to happen, one is to assign the 'id' attr from "a" tag inside this specific "li" tag (the clicked one), to the global variable I have called 'search_r'. 我需要发生两件事,一件事是将特定“ li”标签(单击的那一个)中“ a”标签的“ id”属性分配给我称为“ search_r”的全局变量。 The click event works fine, but to get attribute from "a" tag I can't manage to do. click事件可以正常工作,但是要从“ a”标签获取属性,我无能为力。

Here is my js: 这是我的js:

$("#cc_page").ready(function(){
  $("#search_r").live('click', function(){
      search_r = $(this).attr('id');
      window.location.href = "http://imes.********.com/app/userpanel.html#sfpp_page";
  });
});

I know "this" is not solution. 我知道“这”不是解决方案。 I'm really new to js so that's why I'm asking such an ridiculous questions :) 我对JS真的很陌生,所以这就是为什么我问这么荒谬的问题:)

The first problem you've got is duplicate search_r id attributes, which is invalid. 您遇到的第一个问题是重复的search_r id属性,该属性无效。 These should be changed to classes. 这些应该更改为类。 Also, you should be using on() with a delegate handler, as live() has been removed from the latest version of jQuery. 另外,您应该将on()与委托处理程序一起使用,因为live()已从最新版本的jQuery中删除。 Try this: 尝试这个:

<li class='search_r'>
    <a href='#' id='$id' class='s_result'></a>
</li>
<li class='search_r'>
    <a href='#' id='$id' class='s_result'></a>
</li>
$("#cc_page").on('click', '.search_r', function(){
    var search_r = $('a', this).attr('id');
    console.log(search_r); // just to check it works

    // I assume this is just for testing, otherwise leaving the page 
    // immediately on click renders getting the id completely moot.
    //window.location.href = "http://imes.********.com/app/userpanel.html#sfpp_page";
});

I also assume the $id in your HTML is from some form of templating engine which gets interpreted? 我还假设您HTML中的$id来自某种形式的模板引擎,该引擎会被解释吗? If not, those will also need to be made unique. 如果不是,则还需要使其具有唯一性。

.live has been deprecated in jQuery since v1.7, and has been removed in v1.9. 从v1.7开始, .live在jQuery中已被弃用,而在v1.9中已被删除。

You should replace it with .on() . 您应该将其替换为.on()

.on has 2 syntaxes for binding elements, whereas .live only had 1. .on具有2种用于绑定元素的语法,而.live仅具有1种语法。

If the element exists at the time you are binding, you do it like this: 如果绑定时该元素存在,则可以这样进行:

$('.element').on('click', function(){
    .......
});

You can even use the shorthand: 您甚至可以使用速记:

$('.element').click(function(){
    .........
});

If the element does not exist at the time, or new ones will be added (which is what .live was normally used for), you need to use "event delegation": 如果该元素当时不存在,或者将添加新的元素(通常使用.live ),则需要使用“事件委托”:

$(document).on('click', '.element', function(){
    .............
});

NOTE: You want to bind to the closest static element, not always document . 注意:您想绑定到最接近的静态元素,而不是总是document

In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality if you upgrade your jQuery to the newest version. 同时,如果将jQuery升级到最新版本,则可以使用jQuery Migrate插件来还原.live()功能。

window.location.href causes the browser to make a new request. window.location.href使浏览器发出新请求。 Any variables will be reset when the new page loads. 加载新页面时,所有变量都将被重置。

What is your goal with search_r ? 您对search_r的目标是什么?

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

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