简体   繁体   English

如何使用window.find更改CSS样式?

[英]How do I use window.find to change css style?

Through a combination of AJAX and PHP, I put some text data in a span at the bottom of the page. 通过AJAX和PHP的组合,我将一些文本数据放在页面底部的跨度中。 Now I want to search this text for a string. 现在,我想在此文本中搜索字符串。 My page is full of checkboxes, and their values are the strings I will search for. 我的页面上充满了复选框,它们的值是我要搜索的字符串。

Goal: Using a loop, cycle through the values of all checkboxes on the page. 目标:使用循环,循环浏览页面上所有复选框的值。 Search the page for each checkbox's value (ideally, within the text in the AJAX-informed span). 在页面上搜索每个复选框的值(最好在AJAX通知范围内的文本内)。 If the checkboxes value is found, change that checkboxes CSS style color. 如果找到了复选框的值,请更改该复选框的CSS样式颜色。

My code so far: I have a form full of checkboxes all named "comment" each with unique IDs: 到目前为止,我的代码:我有一个充满复选框的表单,所有复选框均名为“注释”,每个复选框均具有唯一的ID:

<input type="checkbox" name="comment" id="hjl1" value="the comment." 
 onclick="createOrder()"><label for="hjl1" onclick="createOrder()" 
 title="comment"> onscreen text for this checkbox </label>

When triggered , using Javascript, I go through every checkbox in that form. 当使用Javascript触发时,我会检查该表单中的每个复选框。

var comment=document.forms[0].comment;
var txt="";
var ii;
for (ii=0;ii<comment.length;ii++)
  {str=comment[ii].value;}

Now I want to insert window.find in that loop to check if that value is on my page. 现在,我想在该循环中插入window.find,以检查该值是否在我的页面上。

 if (window.find) {
            var found = window.find (str);
            if (!found) { 
         document.getElementById("?????").style["color"] = "red";
           }
        }

The idea is that when the checkbox is checked, the javascript would search for the value "the comment." 想法是,当选中此复选框时,javascript将搜索值“ comment”。 on page. 在页面上。 If found, the checkbox label will add the CSS style color red. 如果找到,复选框标签将添加CSS样式红色。

Somehow, I want to combine these ideas, but there are so many problems. 我想以某种方式将这些想法结合起来,但是有很多问题。 How do I get the element by ID in this loop? 如何在此循环中按ID获取元素? Can window.find search the text created by php in my span? window.find可以在我的范围内搜索php创建的文本吗?

Would it be better to not use window.find at all? 根本不使用window.find会更好吗?

var source = document.getElementsByTagName('html')[0].innerHTML;
var found = source.search("searchString");

I'm so confused and new. 我是如此的困惑和陌生。 Please be patient. 请耐心等待。 Thank you for reading this far. 感谢您阅读本文。

try this as your function code 试试这个作为你的功能代码

function loopy() {  
   var comment=document.forms[0].comment;
   var txt="";
   var ii;
   for (ii=0;ii<comment.length;ii++) {
    if (comment[ii].checked) {
        str=comment[ii].value;
        id = comment[ii].id;

        nextLabelId = comment[ii].nextSibling.id;


        if (window.find) {        // Firefox, Google Chrome, Safari
            var found = window.find (str);
            if (found == true) {
                // found string
                //comment[ii].style['outline']='1px solid red'; 
                document.getElementById(nextLabelId).className = 'selected';                
           } 
        } else {
            // this browser does not support find()
            alert('not supported');
        }
    }
  }
}

So, in order to get the checkbox id, you just add id = comment[ii].id in your loop. 因此,为了获取复选框ID,只需在循环中添加id = comment[ii].id

To change the color, it's best to use class name and use styling in the css file. 要更改颜色,最好在CSS文件中使用类名并使用样式。 so if you want to change the label that is after the checkbox to red you will first find the label's id using nextSiblings and then add the .selected class name. 因此,如果要将复选框后的标签更改为红色,将首先使用nextSiblings查找标签的ID,然后添加.selected类名称。 Just remember that you need to remove the coloring if the user un-check the box 请记住,如果用户取消选中该框,则需要删除颜色

Regarding the usage of find() , not supported by all browser so this could be an issue and also not sure it will be able to find on the content you injected to the DOM by AJAX so this needs some testing. 关于find()的用法,并非所有浏览器都支持,因此这可能是一个问题,并且不确定它是否能够在您通过AJAX注入DOM的内容上找到,因此需要进行一些测试。

I would suggest moving this code to jQuery as some features seems to be easier using their functionality. 我建议将此代码移至jQuery,因为某些功能似乎更易于使用它们的功能。

I misunderstood at first, and wrote code to highlight text within the page. 一开始我误会了,并编写了代码以突出显示页面中的文本。

Yes, window.find is fine to use for this as you only need to know if the value exists or not. 是的,因为您只需要知道该值是否存在,就可以将window.find用于此目的。 It might behave a bit odd (scroll to bottom) when used in frames though. 但是,在帧中使用时,它可能表现得有些奇怪(滚动到底部)。

Also, I added a function for your onClick , but I'm not sure if this is wanted. 另外,我为您的onClick添加了一个功能,但不确定是否需要此功能。 It will change color of the label if text if found when clicked (also). 如果在单击时也找到了文本,它将更改标签的颜色。

Below is a small example: 下面是一个小例子:

 function checkThis(ele) { var str = ele.value; if (window.find) { var found = window.find(str); if (found) { var id = ele.getAttribute('id'); var lbl = document.querySelectorAll('label[for="' + id + '"]'); if (lbl) lbl[0].style.color = "red"; } } } window.onload = function() { var comment = document.form1.getElementsByTagName('input'); for (var x = 0; x < comment.length; x++) { if (comment[x].type == 'checkbox') { var str = comment[x].value; if (window.find) { var found = window.find(str); if (found) { var id = comment[x].getAttribute('id'); var lbl = document.querySelectorAll('label[for="' + id + '"]'); if (lbl) lbl[0].style.color = "red"; } } } } } 
 <form name="form1"> <input type="checkbox" name="comment" id="hjl1" value="the comment." onclick="checkThis(this);" /> <label for="hjl1" onclick="createOrder()" title="comment">onscreen text for this checkbox</label> <input type="checkbox" name="comment" id="hjl2" value="the comment2." onclick="checkThis(this);" /> <label for="hjl2" onclick="createOrder()" title="comment">onscreen text for this checkbox</label> <br/> <b>first comment.</b><br/> <b>other comment.</b><br/> <b>some comment.</b><br/> <b>the comment.</b><br/> <b>whatever comment.</b><br/> <b>not this comment.</b><br/> </form> 

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

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