Javascript 选择器链
基本上,我必须使用jquery更改页面的一部分,但是考虑到页面的格式,我非常困惑选择器链必须是什么Javascript 选择器链,javascript,jquery,Javascript,Jquery,基本上,我必须使用jquery更改页面的一部分,但是考虑到页面的格式,我非常困惑选择器链必须是什么 <span class="foo1"> <span class="intro"><span class="bar">data I need to change</span></1> <span>...</span> <div class="body">This is the only place
<span class="foo1">
<span class="intro"><span class="bar">data I need to change</span></1>
<span>...</span>
<div class="body">This is the only place I can write code on the page</div>
</span>
我需要更改数据
...
这是我唯一可以在页面上编写代码的地方
如何在使用jquery时更改需要更改的数据?显然,我没有服务器访问权限
代码必须以$(this)开头,因为foo中的1总是一个随机数,我猜不出来。根据代码所在的职位,代码必须适用于所有职位
如果我可以使用正常的嵌套,我会这样做。代码必须看起来像
$(this).sibling('.intro').child('.bar').text('bar') 根据您给出的一小部分标记,您可以这样做:
$(".bar").text("Whatever you want it to say instead");
但如果有其他元素与.bar
匹配,则需要更具体:
// All of these would select that element
$(".intro .bar")
$(".foo1 .intro .bar")
$(".intro > .bar")
$(".intro:first-child .bar")
如果需要相对于.body
元素选择它:
// All of these would work
$(".body").siblings(".intro").find(".bar")
$(".body").parent().find(".bar")
我想你明白了除非您扩展您的问题,否则我们无法给您正确的答案。从您给出的小标记片段中,您可以这样做:
$(".bar").text("Whatever you want it to say instead");
但如果有其他元素与.bar
匹配,则需要更具体:
// All of these would select that element
$(".intro .bar")
$(".foo1 .intro .bar")
$(".intro > .bar")
$(".intro:first-child .bar")
如果需要相对于.body
元素选择它:
// All of these would work
$(".body").siblings(".intro").find(".bar")
$(".body").parent().find(".bar")
我想你明白了如果我理解你的问题,除非你扩展你的问题,否则我们无法给你一个正确的答案 只能在
中添加代码。但是你想改变.bar里面的文字
<span class="foo1">
<span class="intro"><span class="bar">data I need to change</span></span>
<span>...</span>
<div class="body">
This is the only place I can write code on the page
<script type="text/javascript">
$('.foo1 .intro .bar').html('Changed!');
</script>
</div>
</span>
我需要更改数据
...
这是我唯一可以在页面上编写代码的地方
$('.foo1.intro.bar').html('Changed!');
您也可以简单地执行
$('.bar').html('Changed')
,但如果您有多个span.bar
,则更安全。如果我理解这个问题
只能在
中添加代码。但是你想改变.bar里面的文字
<span class="foo1">
<span class="intro"><span class="bar">data I need to change</span></span>
<span>...</span>
<div class="body">
This is the only place I can write code on the page
<script type="text/javascript">
$('.foo1 .intro .bar').html('Changed!');
</script>
</div>
</span>
我需要更改数据
...
这是我唯一可以在页面上编写代码的地方
$('.foo1.intro.bar').html('Changed!');
您也可以简单地执行$('.bar').html('Changed')
,但如果您有多个span.bar
,则更安全。此处:
<span class="foo1">
<span class="intro"><span class="bar">data I need to change</span></1>
<span>...</span>
<div class="body">
<script>
$(document).ready(function() { // you need this to make sure document is loaded
$('.foo1 .intro .bar').html("new text");
});
</script>
</div>
</span>
或者-如果在页面底部加载jquery库:
window.onload = function () { /* your code here */ };
在尝试访问jquery和DOM元素之前,请确保它们已就位。此处:
<span class="foo1">
<span class="intro"><span class="bar">data I need to change</span></1>
<span>...</span>
<div class="body">
<script>
$(document).ready(function() { // you need this to make sure document is loaded
$('.foo1 .intro .bar').html("new text");
});
</script>
</div>
</span>
或者-如果在页面底部加载jquery库:
window.onload = function () { /* your code here */ };
在尝试访问jquery和DOM元素之前,请确保它们已就位。
出于嵌套目的,以下是您选择的方式:
$('.foo1 > .intro > .bar').text("text to be changed");
上面的代码表示bar在intro中,intro在foo1中
如果您仍有疑问,请参阅此->
正如其他人所建议的那样
$('.foo1 .intro .bar').html("text to be changed");
这也是进行嵌套的完美方法。
出于嵌套目的,以下是您选择的方式:
$('.foo1 > .intro > .bar').text("text to be changed");
上面的代码表示bar在intro中,intro在foo1中
如果您仍有疑问,请参阅此->
正如其他人所建议的那样
$('.foo1 .intro .bar').html("text to be changed");
这也是进行嵌套的完美方法。
$('.foo1>.intro>.bar').text('Data Changed!');
正如弥赛亚所指出的,你应该使用
$('.foo1>.intro>.bar').text('Data Changed!');
正如弥赛亚所指出的,你应该使用你可以使用的-
$(document).ready(function() {
$('.foo1 .intro .bar').html("new text");
});
您可以使用-
$(document).ready(function() {
$('.foo1 .intro .bar').html("new text");
});
我的问题的正确答案是
<script class="12345">
$('.12345').parents('.foo').find('.intro>.bar').text('whatever');
</script>
$('.12345')。父项('.foo')。查找('.intro>.bar')。文本('whatever');
谢谢你的帮助:)我的问题的正确答案是
<script class="12345">
$('.12345').parents('.foo').find('.intro>.bar').text('whatever');
</script>
$('.12345')。父项('.foo')。查找('.intro>.bar')。文本('whatever');
感谢您的帮助:)我想更改bar类中的一些文本,但我不知道jquery中正确选择bar类的选择器链<代码>$(this).parent('foo1').child('.intro').child('.bar').text('whatever')代码>无效与问题本身无关,但您的HTML无效
div
不是a中允许的内容。为什么在htmlI中使用
,我想否决你的问题,但会给你一个机会,请确保在发布问题之前指定每一点:)接受你认为值得的答案!!!我想更改bar类中的一些文本,但我不知道jquery中正确选择bar类的选择器链<代码>$(this).parent('foo1').child('.intro').child('.bar').text('whatever')代码>无效与问题本身无关,但您的HTML无效div
不是a中允许的内容。为什么在htmlI中使用
,我想否决你的问题,但会给你一个机会,请确保在发布问题之前指定每一点:)接受你认为值得的答案!!!你正确地理解了这个问题(我在编码时使用的语言似乎让那些真正知道自己在做什么的人感到困惑,但却没有让那些不知道如何编码的人感到困惑,这真的很有趣)。然而,我忘了提一下,我必须使用$(这个),因为每一篇包含这个跨度的文章,foo1都会发生变化。但是我只需要在那个特定的帖子上写就行了?foo1,foo2,foo3?不,福总是一个不同的数字,我猜不出来。我不能使用普通选择器,否则我会。如果所有这些跨距没有出现在页面上的多个位置,我只会使用最简单的选择器,但每一个跨距都会在页面上出现多次。您正确理解了这个问题(我在编码时使用的语言似乎会让那些真正知道自己在做什么的人感到困惑,但却不会让那些不知道如何编码的人感到困惑,这真的很有趣)然而,我忘了提到,我必须使用$(这个),因为每一篇包含该跨度的文章foo1都会发生变化。但是我