[英]JQuery selector not finding ID
反对选择器问题,希望有人能提供一些见识。 上下文是我正在开发Mediawiki扩展,该扩展在编辑页面上添加了一些企业特定的功能。 最新的GA MediaWiki包含JQuery 1.11.3。
在此代码段中,我尝试添加带有动态创建表的部分。 大多数代码都可以正常工作,因此我有信心JQuery在环境中可以充分加载。 导致Alert语句的选择器不起作用,该选择器尝试查找可搜索的文本框。 该文本框确实显示在页面上,并且IE和Edge都将ID显示为注释中显示的内容。 选择器的长度总是为零。 我想知道我在想什么。 这是我处理各种引用语法的方式吗? 您可以看到我试图通过使用数组源而不是我要使用的JSON源来简化“源”方面,因此,如果您要测试,请使用“ Charl”作为测试短语。
function addProviders(subjectType, data) {
var title = mw.config.get("wgTitle");
var html = '<div class="mw-portwizard-sourcelist" id = "mw-portwizard-sourcelist"></div>';
if (!$('#mw-portwizard-sourcelist').length) {
$(".templatesUsed").after (html);
}
html = '<div tabindex="0" id="mw-portwizard-sourcelistcontent" class="mw-editfooter-toggler mw-icon-arrow-expanded ui-widget" role="button"><p>Sources available for this page:</p></div>';
$('#mw-portwizard-sourcelist').html(html);
html = '<table id="mw-portwizard-sourcetable"><tbody></tbody></table>';
$('#mw-portwizard-sourcelistcontent').html(html);
var tbody = $("#mw-portwizard-sourcetable").children('tbody');
var table = tbody.length ? tbody : $("#mw-portwizard-sourcetable");
mw.log ("table:" + table);
//TODO: use jquery data to hold the type
//html += '<input type="hidden" value="' + type +'" name="mw-portwizard-sourcetype" id="mw-portwizard-sourcetype" />';
//TODO: Make this WORK!
$.each(data, function(index,value) {
var html = "<tr id='" + value.PortWizard.id +"-row'><td>" + value.PortWizard.url + "</td><td>" + (value.PortWizard.searchable ? '<input type="text" id="' + value.PortWizard.id +'-text" value="' + title + '">' : title) + "</td></tr>";
table.append(html);
if (value.PortWizard.searchable) {
$selector = "#" + value.PortWizard.id + "-text";
// $selector = "#en.wikipedia.org-text";en.wikipedia.org-text
$prompt = $selector + ":" + $($selector).length;
alert ($prompt);
$($selector).autocomplete({
/* source: function(request, response) {
new mw.Api().get( {
action: 'PortWizard',
provider: value.PortWizard.id,
subjectType: 'search',
query: request.term
}).done( function (data){
response(data);
});
},*/
source: ["Charles De Gaulle Airport", "Charlie Brown"],
minLength :3
});
});
}
html += "<button type=\"button\" onclick=\"getPageContent(event,'" + subjectType + "')\">Get Text</button>";
$('#mw-portwizard-sourcelist').append(html);
}
谢谢,
杰森
根据jQuery的选择器文档,您需要进行转义.
有两个反斜杠\\\\.
要将任何元字符(例如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)用作名称的文字部分,必须使用两个反斜杠进行转义:\\。例如,具有id =“ foo.bar”的元素可以使用选择器$(“#foo \\ .bar”)。
所以代替
$selector = "#" + value.PortWizard.id + "-text";
您应该将其更改为此
$selector = "#" + value.PortWizard.id.replace(/\./g, "\\\\."); + "-text";
在选择器中使用元字符不是一个好主意。 如果它们确实存在于选择器中,则@Griffith正确显示了如何处理它们。 作为替代方案,可以使用[id=<id>]
并使用值<id>
代替,而不使用选择器#<id>
:
$('[id="en.wikipedia.org"]')......
这是在构造选择器字符串时可以轻松完成的操作:
$selector = "[id='" + value.PortWizard.id + "-text']";
$('[id="en.wikipedia.org"]').html(function(_, html) { return html + ' FOUND'; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="en.wikipedia.org">TESTING</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.