jquery 检查类是否存在

发布于 2024-12-12 13:38:41 字数 1513 浏览 0 评论 0原文

我有一个 IP 管理脚本,其中包含范围和 jquery 搜索来检查 IP 是否已使用,我向该范围的 IP 的每一行添加类,如下所示

<tr class="table_176 213.5.176.120 213.5.176.121 213.5.176.122 213.5.176.123 213.5.176.124" id="213.5.176.120-124" style="display:none;">
                        <td class="actualip" align="center" >213.5.176.120-124</td>
                        <td >5</td>
                        <td colspan="2" align="center" ><a href="https://www.racksrv.com/portal/staff/clientssummary.php?userid=637" target="_blank"> Mike Burkett</a></td>
                        <td align="center" ><a href="https://www.racksrv.com/portal/staff/clientshosting.php?userid=637&id=1537">RS96</a></td>
                        <td ></td>
                        <td align="center" ><a href="https://www.racksrv.com/portal/staff/supporttickets.php?action=open&userid=637" target="_blank"><img src="https://www.racksrv.com/portal/staff/images/icons/ticketsopen.png" alt="Open Ticket for " title="Open Ticket for " width="16" height="16"/></a></td>
                    </tr>

并使用以下 JQ

$('#search').click(function() {
        $found = false;
        $ip = $('#value').val();
        if ( $("."+$ip).length ){
            alert("found");
        }

        if($found == false) {
            alert('The IP you searched for was not found!');
        }
    });

但这不起作用?有人有什么想法吗?

I have an IP managing script with ranges and jquery search to check if IP is already used, I add class to each row for the IPs for that range, like this

<tr class="table_176 213.5.176.120 213.5.176.121 213.5.176.122 213.5.176.123 213.5.176.124" id="213.5.176.120-124" style="display:none;">
                        <td class="actualip" align="center" >213.5.176.120-124</td>
                        <td >5</td>
                        <td colspan="2" align="center" ><a href="https://www.racksrv.com/portal/staff/clientssummary.php?userid=637" target="_blank"> Mike Burkett</a></td>
                        <td align="center" ><a href="https://www.racksrv.com/portal/staff/clientshosting.php?userid=637&id=1537">RS96</a></td>
                        <td ></td>
                        <td align="center" ><a href="https://www.racksrv.com/portal/staff/supporttickets.php?action=open&userid=637" target="_blank"><img src="https://www.racksrv.com/portal/staff/images/icons/ticketsopen.png" alt="Open Ticket for " title="Open Ticket for " width="16" height="16"/></a></td>
                    </tr>

And use the following JQ

$('#search').click(function() {
        $found = false;
        $ip = $('#value').val();
        if ( $("."+$ip).length ){
            alert("found");
        }

        if($found == false) {
            alert('The IP you searched for was not found!');
        }
    });

But this isn't working? Anyone got any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽心 2024-12-19 13:38:41

正如评论表明您的问题是由类名称中的点引起的,如果您必须将IP地址存储在类名称中,那么类似这样的东西就可以工作 -

   var ip = '213.5.176.120'; 
   if ($("tr[class*='" + ip + "']").length){
        alert("found");
    }

演示 - http://jsfiddle.net/eZCdf/

As the comments suggest your problem is being caused by the dots in your class names, if you have to store the IP addresses in a class name then something like this would work -

   var ip = '213.5.176.120'; 
   if ($("tr[class*='" + ip + "']").length){
        alert("found");
    }

Demo - http://jsfiddle.net/eZCdf/

唐婉 2024-12-19 13:38:41

首先,我非常确定点 (.) 在类名中无效,即使它们有效,我也不会使用它们。这令人困惑
顺便说一句,根据规范,类不能以数字开头。

除了不受 CSS 控制的部分外,所有 CSS 语法在 ASCII 范围内都不区分大小写(即 [az] 和 [AZ] 等效)。例如,HTML 属性“id”和“class”、字体名称以及 URI 的值的大小写敏感性超出了本规范的范围。请特别注意,元素名称在 HTML 中不区分大小写,但在 XML 中区分大小写。
在 CSS 中,标识符(包括选择器中的元素名称、类和 ID)只能包含字符 [a-zA-Z0-9] 和 ISO 10646 字符 U+00A0 及更高版本,加上连字符 (-) 和下划线 ( _);它们不能以数字、两个连字符或连字符后跟数字开头。标识符还可以包含转义字符和任何 ISO 10646 字符作为数字代码(请参阅下一项)。例如,标识符“B&W?”可以写成“B\&W\?”或“B\26 W\3F”。

检查 CSS 语法

其次,你不应该使用类来存储元数据,jQuery 提供了一个方便的方法来做到这一点,它被简单地称为 data()< /a>.

保留这些类应得的,CSS。

First, I'm pretty sure that dot (.) are not valid in class name, and even if they are, I wouldn't use them. That's confusing.
By the way, according to the specs, class couldn't begin with a number.

All CSS syntax is case-insensitive within the ASCII range (i.e., [a-z] and [A-Z] are equivalent), except for parts that are not under the control of CSS. For example, the case-sensitivity of values of the HTML attributes "id" and "class", of font names, and of URIs lies outside the scope of this specification. Note in particular that element names are case-insensitive in HTML, but case-sensitive in XML.
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

Check the CSS Syntax

Second, you shouldn't use class to store metadata, jQuery provides a convenient method to do that, and it's simply called, data().

Keep the classes for what they deserve, CSS.

绝對不後悔。 2024-12-19 13:38:41

正如其他人所说,问题出在你的班级上。很容易将这些点替换为下划线并在类名中使用它们。这是一个快速而肮脏的示例: http://jsfiddle.net/LFfwZ/

As everyone else has said, the problem is your class. It's easy to simply replace those dots with underscores and use those in your class name. Here's a quick and dirty example: http://jsfiddle.net/LFfwZ/

我自己的错误得到了回答:

“$('.213.5.176.120') <=> class="213 5 176 120" 类名中的点应该避免(或在 jQ 选择器中转义)”

my own mistake was answered by

"$('.213.5.176.120') <=> class="213 5 176 120" Dots in classnames should be avoided (or escaped in the jQ selector) "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文