Javascript Jquery-Get元素by id在字符串中构造id
在jquery中使用元素时遇到问题。我在变量中构造名称,例如:Javascript Jquery-Get元素by id在字符串中构造id,javascript,jquery,Javascript,Jquery,在jquery中使用元素时遇到问题。我在变量中构造名称,例如: var myId = "#" + myGotId; $(myId).attr("title", "changed"); $(myId)返回为空。我希望通过id获取元素,但通过动态连接字符串构造id @Pointy编辑-OP在注释中提供的附加代码: var form = $('form#formDefaultValue'); $(':submit', form).click(function(event) { event.pr
var myId = "#" + myGotId;
$(myId).attr("title", "changed");
$(myId)返回为空。我希望通过id获取元素,但通过动态连接字符串构造id
@Pointy编辑-OP在注释中提供的附加代码:
var form = $('form#formDefaultValue');
$(':submit', form).click(function(event) {
event.preventDefault();
$.post(form.attr('action'), form.serialize(), function(data, status, XMLHttpRequest) {
if (status == 'success') {
$("#msgInformation").html("Your changes have been successfully saved.");
jQuery("#spanDefaultValue").attr("class", "db");
var g = indexNodeSelected;
g = g.replace("\\", "\\\\\\\\");
$('#'+ g).find("span").attr("class", "");
}
你需要额外的变量吗?试着不用它,像这样:
$("#" + myGotId).attr("title", "changed");
我不知道到底发生了什么,因为您没有发布太多代码,但请确保您的Javascript代码在DOM中包含具有目标“id”值的元素之后运行。如果您有:
<html>
<head>
<script>
var myId = '#' + myGotId;
$(myId).whatever();
换句话说,只需传递到
$()
函数,就可以在jQuery对象中“包装”普通DOM元素。如果这样做有效,那么问题可能是您的“id”值在某种程度上混淆了选择器引擎。“id”值是否有可能包含“.”?如本例所示,您的代码应该可以工作:
确保在创建要获取的元素后执行脚本。
确保id是正确的
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<div id="mydiv">Hello</div>
<script>
var myGotId = 'mydiv';
var myId = "#" + myGotId;
$(myId).attr("title", "changed");
</script>
</body>
</html>
你好
var myGotId='mydiv';
var myId=“#”+myGotId;
$(myId).attr(“标题”,“更改”);
jQuery无法按预期工作的原因是它在DOM完全加载到浏览器之前执行
将jQuery代码放入文档就绪函数中,它就会工作
$(document).ready(function () {
// code that will run once the entire DOM is loaded into the browser
});
示例网页=>
Firebug控制台输出示例
这里发生了什么事
//run possibly before DOM is loaded into browser
var selector = "#crazy-image";
console.log("DOM may not be ready yet");
console.log($(selector));
$(document).ready(function () {
// code that will run once the entire DOM is loaded into the browser
console.log("DOM ready");
console.log($(selector));
});
应该这样。检查myId的值是否符合预期。myGotId的值是多少?你确定你的意思不是
“#”+“myGotId”
?如果不存在具有该ID的元素,则动态连接字符串或其他方法都不会帮助您。如果David正确,请检查myGotId
的值,以确保它将myId
设置为适当的值。下面是一个代码的工作示例。它不起作用。可能名称中的特殊字符正在影响:var g=indexNodeSelected;g=g.替换(“\\”,“\\”)$(#“+g).attr(“标题”,“更改”);这是我的元素的id,有四个“\\\\”可以使用://$(“\\\\ WindowsAdapter-8CD2DEB979A0188\\\\\TextBox-8CD2DEB9E5CECC4”).attr(“title”,“changed”);当我在variablesWell中动态构造id时,它不起作用。对不起,我发现了问题:我使用了4个反斜杠g=g.replace(“\\”,“\\”);我更改了g=g的替换;现在它工作了。。。!谢谢你的帮助,我的朋友们!!var form=$('form#formDefaultValue')$(':submit',form).单击(函数(事件){event.preventDefault();$.post(form.attr('action')、form.serialize()、函数(数据、状态、XMLHttpRequest){if(状态='success'){$(“#msgInformation”).html(“您的更改已成功保存”);jQuery(#spanDefaultValue”).attr(“类”,“db”);var g=indexNodeSelected;g=g.replace(“\\”,“\\\\\\\\\”;$(“\\\”)+g”).find(“span”).attr(“class”,“);}当我检查firebug中的硬编码值时,它会得到我的元素,当我在inspector和脚本中用$(“\\\\\\\\\\\\\\\\\\\\\\\\”+g)构造元素时,它是空的。什么是“indexNodeSelected”“应该是?定义在哪里?应该是字符串吗?只是一个建议,如果您以前没有尝试过:使用alert()函数在这里和那里查看代码何时被调用,以及myId的值是多少,然后再将其传递给jQuery selector。希望能有所帮助。我使用了一个警报来显示最终构造的Id,它是正确的。我不知道为什么它不能识别它。当我使用firebug检查器时,创建了元素。我想可能是特殊字符id名称中的s正在影响
$(document).ready(function () {
// code that will run once the entire DOM is loaded into the browser
});
//run possibly before DOM is loaded into browser
var selector = "#crazy-image";
console.log("DOM may not be ready yet");
console.log($(selector));
$(document).ready(function () {
// code that will run once the entire DOM is loaded into the browser
console.log("DOM ready");
console.log($(selector));
});