OnClick get“id” “一”的属性标签 [英] OnClick get "id" attribute of "a" tag
问题描述
< 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>
元素数量取决于搜索结果的数量,它不仅仅是这两个。
当我点击列表视图中的元素,在这种情况下:
< li>< / li>
我需要两件事情,一个是从a标签分配idattr在这个特定的li标签(被点击的)中,到全局变量我叫做'search_r'。点击事件工作正常,但要从a标签获取属性,我无法设法执行。
这是我的js:
$(#cc_page)。ready(function(){
$(#search_r)。 (){
search_r = $(this).attr('id');
window.location.href =http://imes.********.com/app /userpanel.html#sfpp_page;
});
});
我知道这不是解决方案。我真的很喜欢js,这就是为什么我提出这样一个可笑的问题:)
已获得重复的 search_r
id属性,这是无效的。这些应该改为课。另外,您应该使用 on()
与委托处理程序,因为 live()
已从最新的版本的jQuery。尝试这样:
< 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 ); //只是检查它的工作
//我假设这只是为了测试,否则离开页面
//立即点击渲染获得id完全模糊
//window.location.href =http://imes.********.com/app/userpanel.html#sfpp_page;
});
我还假设你的$ code $ $ $ HTML是从某种形式的模板引擎得到解释?如果没有,那些也需要被做为唯一的。
I have list view in jQuery Mobile web app this list view is made of elements like this:
<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'. The click event works fine, but to get attribute from "a" tag I can't manage to do.
Here is my 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 :)
The first problem you've got is duplicate search_r
id attributes, which is invalid. 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. 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? If not, those will also need to be made unique.
这篇关于OnClick get“id” “一”的属性标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!