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

查看:112
本文介绍了当元素在标记名称中包含点时,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水印类,它失败了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天全站免登陆