当元素在标签名称中包含点时,Jquery 选择器不起作用 [英] Jquery selector not working when element contains dot in tag name

查看:15
本文介绍了当元素在标签名称中包含点时,Jquery 选择器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次开始使用 jquery,所以我不确定我所做的是否正确.我想做的是非常基本的,我有一个脚本,它在 MVC 视图中加载时向文本框添加 css 水印.

I have just started using jquery for the first time so i'm not sure if what i'm doing is correct. What i'm trying to do is very basic, I have a script which is adding a css watermark to textboxes upon load in an MVC view.

要选择元素,我执行以下操作:

To select the element i do the following:

jQuery(document).ready(function(){$('#Department.DeptName').addWatermark('input-watermarked', 'test');});

然后在我添加 css watermarkclass 的脚本中,它在this.val().length"语句中失败.

Then in my script for adding the css watermarkclass it fails at the "this.val().length" statement.

jQuery.fn.toggleWatermark = function(watermarkedClass, watermarkText) {
if (this.hasClass(watermarkedClass)) {
    this.removeWatermark(watermarkedClass);
}
else if (this.val().length == 0) {
    this.addClass(watermarkedClass);
    this.val(watermarkText);
}

}

脚本在元素 id 为DepartmentDeptName"的情况下工作正常,就好像当元素 id 中包含一个点时选择器不起作用.有谁知道为什么或如何解决这个问题?

The script works fine where an element id is "DepartmentDeptName", it's as if the selector doesn't work when the element id contains a dot inside it. Does anyone know why or how to get around this issue?

推荐答案

我认为你应该用双反斜杠来转义点:$("#Department\.DeptName")此处.

I think you should escape the dot with a double-backslash: $("#Department\.DeptName") See here.

这篇关于当元素在标签名称中包含点时,Jquery 选择器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆