[英]jQuery can't find div with specified id
I am using jQuery to find a div with a specific id, except it return null. 我正在使用jQuery查找具有特定ID的div,但它返回null。 This is strange since trying to find other divs works just fine.
这很奇怪,因为尝试查找其他div效果很好。
Jquery
jQuery的
$(document).ready(function()
{
//Finds parent div without a problem
console.log($('#next-sheet'));
//Returns null, can't find the div I really need
console.log($('#start-course'));
//Also returns null
console.log($('#next-button'));
//Still returns null, can't find the div
console.log($('#next-sheet #start-course'));
});
HTML
的HTML
<div id='next-sheet'>
<a href="/courses/start/<?php echo $data['Courses']['Course']['id']; ?>" class='right disable-hover'>
<div class='button' id='start-course next-button' data-callbackdata="<?php echo $data['Courses']['Course']['name']; ?>" >Start Course</div>
</a>
<div class="clear"></div>
</div>
This is very strange, I am using the .ready function, so the html is available, because it can find the parent div. 这很奇怪,我正在使用.ready函数,因此html可用,因为它可以找到父div。 But when I try to find the child div, it can't find anything.
但是,当我尝试找到子div时,它什么也找不到。
jQuery is loaded correctly, same as all the other stuff. jQuery已正确加载,与所有其他内容相同。 It's all just static HTML.
全部都是静态HTML。 I am using CakePHP, but that should be not the problem.
我正在使用CakePHP,但这应该不是问题。
Is there something I am missing here? 我在这里缺少什么吗?
Since id
is unique, you can only have one id
for an element, so use: 由于
id
是唯一的,因此一个元素只能有一个id
,因此请使用:
id='start-course'
or: 要么:
id='next-button'
instead of: 代替:
id='start-course next-button'
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
ID和NAME令牌必须以字母([A-Za-z])开头,然后可以跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。
id='start-course next-button' is NOT OK. id =“开始课程下一个按钮” 不正确 。 Try with start-course_next-button
尝试使用start-course_next-button
If you want to find control by more than one "name" use classes 如果要通过多个“名称”查找控件,请使用类
<div class="class1 class2"/>
This should be OK 应该没关系
console.log($('.class1'));
console.log($('.class2'));
console.log($('.class1.class2'));
You can't have multiple ids for same element - 同一元素不能有多个ID-
id='start-course next-button' // this is wrong
Although you can have multiple classed like this - 尽管您可以像这样进行多个分类-
class='button start-course next-button' // you will need to replace # with .
You can also try "End with" selector 您也可以尝试使用“结尾为”选择器
$("element[id$='txtTitle']")
ie 即
$("div[id$='next-button']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.