Javascript 当元素在标签名称中包含点时,Jquery 选择器不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3913144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:36:35  来源:igfitidea点击:

Jquery selector not working when element contains dot in tag name

javascriptmodel-view-controllerjquery-selectors

提问by Kevin Quinn

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.

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

To select the element i do the following:

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

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

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

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

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);
}

}

}

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?

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

回答by Joril

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

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

回答by Pedro Gil

You are trying to access the #Departmentwith a class DeptName. You should escape with two backslashes (as Joril said).

您正在尝试#Department使用 class访问DeptName。你应该用两个反斜杠来逃避(正如乔里尔所说)。

See JQuery Selectorsfor more info.

有关更多信息,请参阅JQuery 选择器

回答by Ray

Alternative syntaxes like $("input[name='department.deptname']")will work if you have control over writing jQuery. I am using Spring MVC with Kendo and thus I don't have access to jQuery code. Spring MVC <form>tag automatically puts .whereever applicable. E.g. if the user has Address.. thus field city will become user.address.city(or address.city). And if I break spring MVC into multiple forms then it messes up my back-end logic. It also scatters what should have been a single form. Another alternative is to flatten the Userobject on the back-end... again, not very clean. I am not sure but Dojo worked in such a scenario.

$("input[name='department.deptname']")如果您可以控制编写 jQuery,则可以使用替代语法。我在 Kendo 中使用 Spring MVC,因此我无法访问 jQuery 代码。Spring MVC<form>标签会自动放置.在适用的地方。例如,如果用户有地址.. 因此字段城市将成为user.address.city(或address.city)。如果我将 spring MVC 分解为多种形式,那么它就会弄乱我的后端逻辑。它还分散了本应是单一形式的内容。另一种选择是User将后端的对象展平......同样,不是很干净。我不确定,但 Dojo 在这种情况下工作。