如何使用 JavaScript 从 HTML 中提取信息

how to pull information out of html with javascript

本文关键字:提取 信息 HTML 何使用 JavaScript      更新时间:2023-09-26

我有这组代码,它将链接到hta页面的各个部分:

<ul>
    <li><strong>Windows 10</strong> comes with a new design and features.</li><br><br>
    <li><strong><a href="#" class="startLinks" id="Edge">Microsoft-Edge</a></strong> has been introduced as Microsoft's next generation web browser.</li><br><br>
    <li><strong><a href="#" class="startLinks" id="Skype">Microsoft Skype for Business</a></strong> is the Lync that you know . </li><br><br>
    <li><strong><a href="#" class="startLinks" id="FindPrinter">Printer list</a></strong> ........... </li><br><br>
    <li><strong><a href="#" class="startLinks" id="Email">Email Signature</a></strong> ........... </li>
</ul>

我希望Microsoft边缘,Microsoft Skype for Business,打印机列表和电子邮件签名作为链接,并且根据单击的链接,JavaScript将在此处使用slider.gotoslide(x)

    $('div.Welcome a').click(function(){
        var itemClicked = 'div.Welcome a';
        if (itemClicked.index() === 0){  //Microsoft Edge
            slider.goToSlide(6); //slide 6
        }
        else if (itemClicked.index() === 1) { //Microsoft Skype for Business
            slider.goToSlide(10); //slide 10
        }
        else if (itemClicked.index() === 2) { //Find and Add Printers
            slider.goToSlide(15); //slide 15
        }
        else if (itemClicked.index() === 3) { // Email Signature
            slider.goToSlide(11); //slide 11
        }
        return false;
    });

如您所见,我需要div.welcome a给我单击了哪个链接,然后是 if 语句来告诉我要转到哪张幻灯片。然而,指数说

对象不支持属性或方法"索引"

使用 $(this) .

使用上面的表达式,您将使用 jQuery 转换/包装调用的元素(this)(在本例中单击锚标记)。因此,此表达式的结果将是一个 jQuery 对象。

$(function(){
   $('div.Welcome a').click(function(e){
       e.preventDefault();  //prevent default link click behaviour
       var itemClicked = $(this);
       var id = itemClicked.attr("id");  //get the Id of the clicked a tag
       alert(id);
      //use id for your further code now
   });
});

下面是一个工作 jsBin 示例。

我觉得你需要改变的只是

var item = 'div.welcom a' to $('div.Welcome a');