简体   繁体   English

如何使用类和内部p标签定位div

[英]How to target a div with a class and an inner p tag

I have a structure that looks like this.. 我有一个看起来像这样的结构。

<div class="infobubble">
    <a href="#" class="btn-closebubble"></a>
    <p>
PLACE CONTENT HERE
</p>
</div>

How do I use jquery to target the in 我如何使用jQuery来定位

tags? 标签?

I tried this but did not work. 我尝试了这个,但是没有用。

$("infobubble p").html('My Text');

Your code is wrong, you need to specify that you are looking for a class, like so 您的代码是错误的,您需要指定要查找的类,如下所示

$(".infobubble p").html('My Text');

Your code $("infobubble p") would be looking for a tag element named infobubble which does not exist 您的代码$("infobubble p")将寻找一个不存在的名为infobubble的标记元素

$(".infobubble p").html('My Text');

in jQuery you need to add an '.' 在jQuery中,您需要添加“。” to select by class or '#' to select by id. 按类别选择,或按“#”按ID选择。 If no '.' 如果不 '。' or '#' is specified, jQuery will try to find an element by tag name... in your case it was trying to find <infobubble> which isn't an element. 或指定了“#”,jQuery将尝试通过标签名称查找元素...在您的情况下,它试图查找不是元素的<infobubble>

如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使选择器选择div类且标签名称为p? - How to make selector choose div class and with tag name of p? 如何以此为目标标记内部div? - How to target tag a inside div with this? 如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag? 如何在课后定位标签? - How to target tag after a class? 如何在div内获取带有标签的内部HTML? - How to get a tag inner html with is inside a div? 如果“ p”标签为空,如何隐藏div贴图 - How to hide a div map if “p” tag is empty 如何在 DIV 标签中设置目标 =“_blank”? - How to set a target="_blank" in a DIV tag? 当 class 添加到单独的 div 时更改 p 标签文本 - Change p tag text when class is added to a separate div div 标签目标属性 - Div tag target attribute 如何按类别和该div中的类别定位/选择div - How to target/select div by class and class within that div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM