[英]How do you first find a class that contains a string and then get the id of the element with that class you just found? Using Javascript
What I'm trying to do is search a page for an element that has the string 'tab_selected' in the class name. 我想做的是在页面上搜索在类名中具有字符串“ tab_selected”的元素。 The class name is more than just 'tab_selected'.
类名不仅仅是“ tab_selected”。 Its 'tab_selected + the UUID'.
它的“ tab_selected + UUID”。 The UUID changes so all I really want to search for is if the class name contains 'tab_selected' and then if it does I want to get the ID of that element and set the cookie with that ID.
UUID发生更改,因此我真正要搜索的是类名是否包含“ tab_selected”,然后如果要这样做,则要获取该元素的ID并使用该ID设置cookie。 I'm a terrible Noob with javascript so any direction would be awesome.
我是一个使用JavaScript的可怕的Noob,所以任何方向都很棒。 Thanks!
谢谢!
EDIT: 编辑:
Thats actually fairly simple with jQ: 使用jQ实际上相当简单:
var sel = '.tab_selected_left_horizontal, .tab_selected_right_horizontal, .tab_selected_text_horizontal, .tab_selected_left_vertical, .tab_selected_text_vertical, .tab_selected_right_vertical';
var ids = $(sel).map(function(){
return $(this).attr('id');
});
I would recommend adding an additional class of tab_selected
without the UUID. 我建议添加一个没有UUID的
tab_selected
附加类。 This way you can grab all the elements of that class without having to parse class names. 这样,您无需解析类名即可获取该类的所有元素。 So you element might look like this:
所以您的元素可能看起来像这样:
<div class="tab_selected tab_selected-UUID"></div>
This way you can just do $('.tab_selected')
. 这样,您可以只执行
$('.tab_selected')
。
Just a guess, but if you're trying to save a cookie from your selected tab jquery can do it for your: 只是一个猜测,但是如果您尝试从所选选项卡中保存cookie,则jquery可以为您执行以下操作:
You will need, jquery-ui and a cookie plugin for it, then all you have to do is change your tabs initialization: 您将需要jquery-ui和一个cookie插件,然后您要做的就是更改选项卡的初始化:
<script type="text/javascript">
$(function() {
$("#tabs").tabs({
cookie: {
// store cookie for a day, without, it would be a session cookie
expires: 1
}
});
});
</script>
Ok it was much more simple than I was making it. 好吧,这比我做的要简单得多。 Thanks everyone for your help!
谢谢大家的帮助! Here was my test and it worked perfectly.
这是我的测试,效果很好。
$(document).ready(function() {
$(".tab_selected_text_horizontal").attr("id");
var title = $(".tab_selected_text_horizontal").attr("id");
alert(title);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.