简体   繁体   English

如何检查Selection对象是否包含两个同级“ Div”或“ P”

[英]How to check if the Selection object contains two sibling 'Div' or 'P'

I want to check a condition in which whether the user selection has any two or more div or paragraph sibling in it. 我想检查用户选择中是否有两个或多个div或段落同级的条件。 am breaking my head on this am new to dom stuffs. 在这对dom东西很陌生的方面让我很头疼。 Anybody came across such situation before or somebody can help me with the algorithm or logic to achieve this . 任何人都曾经遇到过这种情况,或者有人可以帮助我实现该目标的算法或逻辑。 any help is highly appreciated. 非常感谢您的帮助。

Whatever you asked here, please share code snippet. 无论您在此处提出什么要求,请共享代码段。 Here i make for you example to achieve that. 在这里,我为您提供实现此目标的示例。 Since you did't post any code, then i'm not sure the example provided meet the expectations. 由于您未发布任何代码,因此我不确定所提供的示例是否符合预期。

HTML HTML

<div id="a">Click me
   <p></p>
   <div></div>
   <div></div>    
</div>

JS JS

$('#a').on('click', function(){
  var div = $(this).children('div').length;
  var p = $(this).children('p').length;

  if(div >= 2)
  {
    alert('div exists : ' + div);
    //do your stuff here
 }
 else
 {
    alert('i have div below than two');
    //do your stuff here if below than two
 }

//same goes with p
});

DEMO DEMO

如何检查是否<div>包含确定的文本?</div><div id="text_translate"><p> 我想为我的网站制作一个复活节彩蛋。 有一个可以更改的标题。 如果它包含确定的字符串,我希望它启动easteregg。 </p><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-html lang-html prettyprint-override"> &lt;h1 class="titlename" id="changeT"&gt;Welcome&lt;/h1&gt; &lt;div class="action1"&gt; &lt;div id="titlechange"&gt; &lt;p&gt; &lt;div id="explain"&gt;Write the title you want and press "Confirm". The title "Welcome" will be replaced with the text you wrote. &lt;span class="material-icons"&gt; leaderboard &lt;/span&gt;&lt;/div&gt; &lt;/p&gt; &lt;input type="text" id="myText" value="Your title" maxlength="50"&gt; &lt;button onclick="chageText()" class="hideshowbtn"&gt;Confirm &lt;span class="material-icons"&gt; check &lt;/span&gt;&lt;/button&gt; &lt;script&gt; function chageText() { var x = document.getElementById("myText").value; document.getElementById("changeT").innerHTML = x; } &lt;/script&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p><p> 如果包含“hello”,我希望它启动一个脚本。 如何?</p></div> - How to check if a <div> contains a determinate text?

暂无
暂无

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

相关问题 检查选择是否包含div Jquery - check if selection contains div Jquery 隐藏包含的div <p> 包含“文本” - 如何 - hide div that contains <p> that contains “text” - how to 如何检查get Selection是否仅包含空格 - how to check if get Selection contains only whitespace 检查选择是否包含链接 - Check if selection contains link 检查选择是否包含图形 - Check if the selection contains graphics 如何检查div是否包含某些文本? - How to check if div contains certain text or not? 如何检查div包含哪个背景图像? - How to check which background image div contains? 如何检查是否<div>包含确定的文本?</div><div id="text_translate"><p> 我想为我的网站制作一个复活节彩蛋。 有一个可以更改的标题。 如果它包含确定的字符串,我希望它启动easteregg。 </p><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-html lang-html prettyprint-override"> &lt;h1 class="titlename" id="changeT"&gt;Welcome&lt;/h1&gt; &lt;div class="action1"&gt; &lt;div id="titlechange"&gt; &lt;p&gt; &lt;div id="explain"&gt;Write the title you want and press "Confirm". The title "Welcome" will be replaced with the text you wrote. &lt;span class="material-icons"&gt; leaderboard &lt;/span&gt;&lt;/div&gt; &lt;/p&gt; &lt;input type="text" id="myText" value="Your title" maxlength="50"&gt; &lt;button onclick="chageText()" class="hideshowbtn"&gt;Confirm &lt;span class="material-icons"&gt; check &lt;/span&gt;&lt;/button&gt; &lt;script&gt; function chageText() { var x = document.getElementById("myText").value; document.getElementById("changeT").innerHTML = x; } &lt;/script&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p><p> 如果包含“hello”,我希望它启动一个脚本。 如何?</p></div> - How to check if a <div> contains a determinate text? 如何通过单击包含它的 div 来选中/取消选中复选框? - How to check/uncheck a checkbox by clicking a div that contains it? 如何检查一个对象是否包含另一个对象? - How to check if an object contains another object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM