jquery 检查类是否存在
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如评论表明您的问题是由类名称中的点引起的,如果您必须将IP地址存储在类名称中,那么类似这样的东西就可以工作 -
演示 - 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 -
Demo - http://jsfiddle.net/eZCdf/
首先,我非常确定点 (.) 在类名中无效,即使它们有效,我也不会使用它们。这令人困惑。
顺便说一句,根据规范,类不能以数字开头。
检查 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.
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.
正如其他人所说,问题出在你的班级上。很容易将这些点替换为下划线并在类名中使用它们。这是一个快速而肮脏的示例: 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) "