简体   繁体   English

您如何首先找到一个包含字符串的类,然后使用刚刚找到的那个类获取元素的ID? 使用JavaScript

[英]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>

Cookie plugin is here Cookie插件在这里

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.

相关问题 您如何在不为其分配类/ ID的情况下访问列表元素? - How do you access a list element without assigning a class/id to it? 你怎么知道HTML元素是否有一个普通的Javascript类? - How do you find out if an HTML element has a certain class with plain Javascript? 如何在 VSCode JavaScript 中获取 HTML 元素 ID 以自动完成? - How do you get an HTML element ID to autocomplete in VSCode JavaScript? 如何在JavaScript中获取单击的div元素的ID? - How do you get an ID of a clicked div element in JavaScript? 您如何仅选择javascript中元素具有的第二类? - How do you only select the second class an element has in javascript? 如何将元素添加到 class(通过 JavaScript)? - How do you add an element to a class (through JavaScript)? 如何从Angular中的元素获取类? - how do you get a class off an element in Angular? 如何使用Javascript从外部HTML文档中按ID获取元素? - How do you get an element by ID from an external HTML document using Javascript? 使用 javascript,如何从元素中的某些 class 名称中跳过或删除 e.stopPropagation()? - Using javascript, how do you skip, or remove, e.stopPropagation() from certain class names in an element? 您如何获取JavaScript中所有班级孩子的列表? - How do you get a list of all class children in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM