简体   繁体   English

如何使用JavaScript针对一组标签文本检查所有“ a”元素内部文本是否匹配?

[英]How can I check all “a” elements inner text against a set of labels text for matches using JavaScript?

I have a bunch of "a" elements on my page that hold the day of the month for a calendar. 我的页面上有一堆“ a”元素,它们保存日历的月份。 I have hidden labels, each containing a day of the month. 我有隐藏的标签,每个标签都包含一个月中的某天。 I want to use JavaScript to check of there is a match. 我想使用JavaScript检查是否存在匹配项。 If there is a match, I want to add HTML nested within the a element. 如果存在匹配项,我想添加嵌套在a元素内的HTML。

Here is what I have tried, but it does not work as expected: 这是我尝试过的方法,但未按预期工作:

<span id="eventDayLabel1" class="daysClass">18</span>
<a href="javascript:__doPostBack('ctl00$MainContent$Calendar2','6317')" style="color:Black" title="April 18">18</a>

$(function () {
            $('a').on('click', function (event) { event.preventDefault(); });
        });


var elemAs = document.getElementsByTagName('a');
        for (var i = 0; i < elemAs.length; i++) {


       var elemA = elemAs[i];
            var dayLabels = document.getElementsByTagName('daysClass');
            function contains(dayLabels, elemA) {
                for (var i = 0; i < dayLabels.length; i++) {
                    if (dayLabels[i].innerText == elemA.innerText) {
                        // add HTML here
                    }
                }
                return false;
            }
        }

Why doesn't this piece of code work? 为什么这段代码不起作用? I've checked that the labels and a elements are being created there are matches (using inspect element in Chrome). 我检查了标签和创建的元素是否匹配(使用Chrome中的inspect元素)。

You aren't calling the contains() function and you're using getElementsByTagName() to get a class . 您没有调用contains()函数,而是使用getElementsByTagName()获取一个class

Just remove the function and the return false , and use getElementsByClassName() instead. 只需删除该函数并return false ,然后使用getElementsByClassName()

 var elemAs = document.getElementsByTagName("a"); for (var i = 0; i < elemAs.length; i++) { var elemA = elemAs[i]; var dayLabels = document.getElementsByClassName("daysClass"); //function contains(dayLabels, elemA) { for (var i = 0; i < dayLabels.length; i++) { if (dayLabels[i].innerText == elemA.innerText) { console.log('match'); } } //return false; //} } 
 <span id="eventDayLabel1" class="daysClass">18</span> <a href="javascript:__doPostBack('ctl00$MainContent$Calendar2','6317')" style="color:Black" title="April 18">18</a> <span id="eventDayLabel1" class="daysClass">18</span> <a href="javascript:__doPostBack('ctl00$MainContent$Calendar2','6317')" style="color:Black" title="April 18">17</a> <span id="eventDayLabel1" class="daysClass">19</span> <a href="javascript:__doPostBack('ctl00$MainContent$Calendar2','6317')" style="color:Black" title="April 18">19</a> 

暂无
暂无

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

相关问题 如何检查文本输入是否与变量匹配? - How can I check if text input matches a variable? 如何使用带有 JavaScript 的 XPATH 选择与元素内部文本的一部分匹配的元素 - How to select an element that matches a part of the element's inner text using XPATH with JavaScript 通过内部文本获取所有元素 - Get all elements by inner text 如何在JavaScript中使用全日历查看一天中的所有文本? - How can I see all text in the day using fullcalendar in javascript? 如何使用JavaScript设置此ASPX控件的文本? - How can I set the text of this ASPX control using JavaScript? 使用javascript或jQuery隐藏所有带有text或innerHTML的&#39;a&#39;元素,这些元素匹配数字&#39;0&#39;或自定义值 - Hide all 'a' elements with text or innerHTML that matches the number '0' or a custom value using javascript or jQuery 如何让我的文字标签始终面向相机? 也许使用精灵? - How can I make my text labels face the camera at all times? Perhaps using sprites? 如何使用JavaScript访问表单选择标签的value属性的内容或其内部文本? - How can I acces to the content of the value attribute of a form select tag or to its inner text using JavaScript? 如何使用JavaScript访问Div标签的内部Text? - How do i access inner Text of Div tag using javascript? 如何通过JavaScript的正则表达式获取元素内部文本中的数字 - How to get numbers in elements' inner text by javascript's regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM