使用 Javascript 选择特定标签内的标签

发布于 2024-10-17 02:12:49 字数 1700 浏览 1 评论 0原文

我在尝试将 chrome 的小用户脚本放在一起时遇到两个问题。 目标是从论坛中查找并汇总信息。 html 的结构类似于以下内容:

<div class="wrapper">
 <div class="cfInnerWrapper">
  <div class"inner" title="user1">user1</div>
  <div class="cfMessage">
   <div class="message">  
    This is a message. <strong>the important information 1</strong>
   </div>
  </div>
 </div>
</div>
<div class="wrapper">
 <div class="cfInnerWrapper">
  <div class"inner" title="user2">user2</div>
  <div class="cfMessage">
   <div class="message">  
    This is a message. <strong>the important information 2</strong>
   </div>
  </div>
 </div>
</div>

我只想获取 中的重要消息。并将其与说出这句话的用户联系起来。重要消息还应该包含特定的字符串,但这不是这里的问题。为此,我找到所有 cfInnerWrapper,然后一一浏览它们以选择重要的消息用户对并将它们保存在数组中。

这是我的 JS:

window.onload = function(){
 var find_str = /important/mi;
 var votes = new Array();
 var message = document.getElementsByClassName('cfInnerWrapper');
 for (m in message){
  var strong_element = message[m].getElementsByTagName('strong'); <---- Error
  if(strong_element){
   var strong_content = strong_element[0].innerHTML;
   if(strong_content.match(find_str)){
    var message_user = message[m].getElementsByClassName('inner')[0].getAttribute('title');
    votes[message_user] = strong_content;
   }
  }
  alert(votes['user1']); <--- Works
 }
 alert(votes['user1']); <--- Doesn't work
}

好的,这确实有效。但我的浏览器(即 Chrome)在我显示它时说它是一个错误。 有没有更好的方法从特定标签中选择标签?

下一个问题是,由于某种原因,保存的数组似乎没有保存在 for 循环之外。 当我在 for 循环中提醒投票内容时,我可以访问该信息。当我尝试从循环外部执行此操作时,它根本不起作用。我不明白为什么。

I have two problems with a small userscript for chrome that I'm trying to put together.
The goal is to find and put together information from a forum.
The structure of the html is similar to the following:

<div class="wrapper">
 <div class="cfInnerWrapper">
  <div class"inner" title="user1">user1</div>
  <div class="cfMessage">
   <div class="message">  
    This is a message. <strong>the important information 1</strong>
   </div>
  </div>
 </div>
</div>
<div class="wrapper">
 <div class="cfInnerWrapper">
  <div class"inner" title="user2">user2</div>
  <div class="cfMessage">
   <div class="message">  
    This is a message. <strong>the important information 2</strong>
   </div>
  </div>
 </div>
</div>

I want to get only the important message within the <strong> and connect it to the user saying it. The important message should also contain a specific string, but that not an issue here. I do this by finding all cfInnerWrapper and then go through them one by one to select the important message-user-pair and saves them in an array.

This is my JS:

window.onload = function(){
 var find_str = /important/mi;
 var votes = new Array();
 var message = document.getElementsByClassName('cfInnerWrapper');
 for (m in message){
  var strong_element = message[m].getElementsByTagName('strong'); <---- Error
  if(strong_element){
   var strong_content = strong_element[0].innerHTML;
   if(strong_content.match(find_str)){
    var message_user = message[m].getElementsByClassName('inner')[0].getAttribute('title');
    votes[message_user] = strong_content;
   }
  }
  alert(votes['user1']); <--- Works
 }
 alert(votes['user1']); <--- Doesn't work
}

Ok, so this actually works. But my browsers ie Chrome says its an error where I show it.
Is there a better way to select a tag from within the specific tag?

The next problem is that the saved array doesn't seem to be saved outside the for-loop for some reason.
When I alert the content of vote within the for-loop I can access the information. When I try to do it from outside the loop it doesn't work at all. I don't understand why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-10-24 02:12:49
  • 首先,您在数组或类似数组(在本例中为 NodeList)对象上使用 for (item in collection) 构造,这是导致错误的主要原因。

    这不是正确的方法,并且当您迭代到 length 属性时会出现错误,该属性是一个数字,而不是一个元素。

    相反,您需要使用 for 循环或将其转换为数组,然后对其进行 forEach

  • 您将投票存储在数组中,但您将其用作关联数组,因此您应该将它们存储在对象中。

    请注意,您可以使用 [] 创建新数组,使用 {} 创建新对象。我提到这一点是因为您使用了 new Array()

  • 使用querySelectorquerySelectorAll可以让你的代码更短一些!您正在创建一个 Chrome 用户脚本,Chrome 已经支持它。

所以这里有一个改进的代码,它更具可读性并且更有效。 jsFiddle

var messages = document.querySelectorAll('.cfInnerWrapper'),
    votes = {},
    pattern = /important/i;

// for each message
for (var i = 0; i < messages.length; i ++) {

    var message = messages[i],
        user = message.querySelector('.inner[title]'),
        strong = message.querySelector('strong');

    // check that the elements exist
    if (user && strong) {

        var userName = user.getAttribute('title'),
            content = strong.innerHTML;

        // check if it matches the pattern
        if (pattern.test(content)) {
            votes[userName] = content;
        }

    }

}
  • First, you are using for (item in collection) construct over an array or an array-like (in this case NodeList) object, which is the main cause of your error.

    This is not the right way to do, and will give you error when you it iterates to the length property, which is a number, not an element.

    Instead, you need to use the for loop or convert it to an array and then forEach on it.

  • You are storing votes inside an Array, but you are using it like an associative array, you should store them in an Object instead.

    Note that you can use [] to create a new Array and {} to create a new Object. I mentioned this because you used new Array().

  • Using querySelector and querySelectorAll makes your code a bit shorter! You are creating a Chrome userscript and Chrome already supports it.

So here is an improved code which is more readable and works. jsFiddle

var messages = document.querySelectorAll('.cfInnerWrapper'),
    votes = {},
    pattern = /important/i;

// for each message
for (var i = 0; i < messages.length; i ++) {

    var message = messages[i],
        user = message.querySelector('.inner[title]'),
        strong = message.querySelector('strong');

    // check that the elements exist
    if (user && strong) {

        var userName = user.getAttribute('title'),
            content = strong.innerHTML;

        // check if it matches the pattern
        if (pattern.test(content)) {
            votes[userName] = content;
        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文