简体   繁体   English

仅对输入单词匹配的那段使用正当性边界函数

[英]Use a justificative bordering function only for that paragraph in which the input word is matching

So, i want to border the correct paragraph's div, when i input a word like "Security" for example, and then the 3rd, the "comment2" div will be bordered. 因此,我想在正确段落的div处加上边框,例如当我输入“ Security”之类的单词时,然后在第3个空格处,将“ comment2” div加上边框。 What i would like: Only the respective div needs to be bordered, where the word appearing. 我想要的是:只有相应的div需加边框,单词出现在此处。 The function is working, but the problem is that all the divs are bordered after i submit the matching word. 该功能正常工作,但问题是我提交匹配的单词后所有div都带有边框。

 function bordering1() { var text = document.getElementById("texthere").textContent; var inputText = document.getElementById("commentsec"); var innerHTML = inputText.innerHTML; var index = innerHTML.indexOf(text); var n = document.getElementsByClassName("commentdiv"); for (var i = 0; i < n.length; i++) { if (index > 0) { n[i].setAttribute("style", "border: 1px solid blue;"); } } } 
 <div class="col-md-8 col-md-offset-2 bordered" id="commentsec"> <div class="col-md-12 bordered commentdiv" id="comment0"> <div class="col-md-10 para bordered" id="paragraphdiv"> <p id="firstcomment">Significantly Reduce Costs and Improve Quality with an Experienced, Professional Global Coding Solution. Health Information Management (HIM) Directors and CFOs are seeking innovative ways to reduce expenses, maintain DNFB goals, and increase overall coder quality.</p> </div> </div> <div id="comment1" class="col-md-12 bordered commentdiv"> <div id="paragraphdiv" class="col-md-10 para bordered"> <p id="secondcomment">Sacrificing quality is something we refuse to do at Peak and that's a key component in our Global Coding success.</p> </div> </div> <div id="comment2" class="col-md-12 bordered commentdiv"> <div id="paragraphdiv" class="col-md-10 para bordered"> <p id="secondcomment">Security of Patient Health Information (PHI), Data, Network, Hardware, Software and Physical Infrastructure are all top priorities for Peak.</p> </div> </div> </div> 

I think you kind of had it, it was the index > 0 check going wrong which would always pass if the word was found anywhere. 我认为您有点喜欢它,就是索引> 0检查出错,如果在任何地方找到该单词,该错误总是会通过。

How about if you check each section individually for the word and then apply the border then? 如果您单独检查每个部分的单词,然后加上边框,该怎么办?

Here's a fiddle: https://jsfiddle.net/kelvinsusername/k3d2r9xy/5/ 这是一个小提琴: https : //jsfiddle.net/kelvinsusername/k3d2r9xy/5/

I just changed the JS a bit so when looping through the sections it checks for the word then: 我只是稍微更改了JS,所以在遍历各节时会检查单词:

  for (var i = 0; i < n.length; i++) {
     var index = n[i].innerHTML.indexOf(text);

     if (index > 0) {
        n[i].setAttribute("style", "border: 1px solid blue;");
     }
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM