Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery选择这个div并更改html?_Jquery - Fatal编程技术网

如何使用jQuery选择这个div并更改html?

如何使用jQuery选择这个div并更改html?,jquery,Jquery,我有以下html: <div class="scroller-pane"> <div class="scroller-header"></div> <div class="scroller-division" id="0"> <div style="height:20px;"></div> <div class="title">A day with bears<

我有以下html:

<div class="scroller-pane">
    <div class="scroller-header"></div>
    <div class="scroller-division" id="0">
        <div style="height:20px;"></div>
        <div class="title">A day with bears</div>
        <div class="date">30/06/2011</div>
        <div class="clear"></div>
        <div class="text">Lorem Ipsum is simply dummy text of the printing...</div>
        <div style="float:right;"><a href="htp://www.google.com">more>></a></div>
    </div>
<div>


但我得到的只是“未定义”,为什么这些select语句都不起作用?如何编写jQuery select语句以访问“title”div.?

如果您有元素的ID,只需通过以下方式将其作为目标:

console.log($('#0').children('div.title').html());
我打过电话:

console.log($('.scroller-pane .scroller-division #0 .title').html());
console.log($('.scroller pane.scroller division#0.title').html())

也就是说,“查找所有具有类“title”的元素,这些元素是id为
id
“0”的元素的后代,而“scroller division”是具有类“scroller pane”的元素的后代,但id为“0”的元素是“scroller division”,而不是它的后代

如果您真的希望仅当id为
0”的元素以这种方式嵌套时,才以此为目标,并且不想以其他方式执行任何操作,则可以执行以下操作:

console.log($('.scroller-pane .scroller-division .title').html());

(最后一个将忽略id为
的元素,如果它们没有类“scroller division”。)

但是,如果您想将
id
“0”的“title”子体作为目标,而不管它在哪里,它会变得更短:

console.log($('#0 .title').html());


非主题强烈建议不要使用“0”作为
id
值。它是,但它是和更早的(
id
不能以数字开头),它是(相同的)。

您可以使用
$(“#id”)通过id访问它
但是,我认为不允许将数字作为id的第一个字符

我不确定您是否要更改html或检索它,但以下是两者的示例:


我不认为你可以有一个以数字开头的
id
。阅读这里的部分:了解更多信息:@Daniel:w3schools已经过时。“0”在HTML5中是一个有效的
id
。它在HTML4.01和更早版本中无效,在CSS中无效,我永远不会使用它,但在HTML5中是有效的(参考链接见我的答案)
javascript
中的
id
不能以数字开头
id
中的
javascript
不能以数字开头numbers@Miroprocessor:JavaScript对
id
值没有任何规定。HTML和CSS有规定。奇怪的是,我只是向作者添加了一条建议,不要使用“0”“作为一个ID,但它在HTML5中是有效的(它在HTML4.01或CSS中无效)。我认为鉴于我使用数字作为ID的一般语气,它不是一个好的IDE…@T.J.Crowder我不知道它在HTML5中是有效的,但我作者没有提到他是否以任何方式使用HTML5,谢谢你的新建议info@Pete2k:是的,正如我上面所说,我强烈建议不要使用它。只要在它前面加上一个“x”或别的什么(例如,
x0
),那么它在所有三个标准中都是有效的。:-)
console.log($('.scroller-pane #0 .title').html());
console.log($('.scroller-pane .scroller-division[id=0] .title').html());
console.log($('#0 .title').html());
$(document).ready(function() {
    alert($('.scroller-pane .scroller-division[id=0] .title').html());
    $('.scroller-pane .scroller-division[id=0] .title').html('test');
});
console.log($('.scroller-pane .scroller-division#0 .title').html());