简体   繁体   中英

how can i grab a text from a href in a website (page source) using javaScript?

This is the part of page source of a website. I need to grab the " crsEdId=78 " in the last line. Using JavaScript or JQuery.

I will use mozilla Jetpack to do the task.

How can i Get this Code first? and What to do (RegEx or JQuery) on it !?

<div class="tools-menu-body" id="tools-menu-div" style="display: none;">
        <div id="menu_glider">
            <div class="scroller">
                <div class="content">
                     <div class="section" id="courses_menu">
                        <ul class="tools-menu-ul">
                            <li class="tools-menu-back"><a href="#main_menu" class="controls">back</a></li>
<li><a title="Advanced Software Engineering Lab: Open Source Development" href="../Courses/CourseEdition.aspx?crsEdId=78">CSEN 914 </a></li>

IT WORKED :

well it was a stupid thing - i just had to put the selector inside the click function, donno why i didn't think about it earlier.

  jetpack.statusBar.append({
  html: "Boom<i>!</i>",
  width: 45,
  onReady: function(widget){
      $(widget).click(function(){
             var doc= jetpack.tabs.focused.contentDocument;
             var link= doc.querySelector('#courses_menu > ul > li:nth-child(2) a');
             var test= doc.getElementById('#container'); 
             jetpack.notifications.show("Course is is = "+ link);

});
}
});    

Now i need to get all the href inside this courses_menu !! Can you help in that ?

也许我缺少了一些东西,但是您不能只获取该元素的href,然后使用RegEx来获取所需的内容吗?

在jQuery中:

var matches = $('a').attr('href').match(/crsEdId=(\d+)/);

尝试:

alert($('#courses_menu').children('li').get(2).attr('href'));

If you've got the document in the current tab, I believe Jetpack gives it to you like:

var doc= jetpack.tabs.focused.contentDocument;

Then in plain JS, get hold of the link by whatever means is most appropriate. For example to get the first link from the second list item:

var menu= doc.getElementById('courses_menu');
var link= menu.getElementsByTagName('li')[1].getElementsByTagName('a')[0];

If you are using Jetpack you can presumably rely on a new enough version of Firefox to have native support for the Selectors-API, so you could get this more easily without the use of an additional library such as jQuery, eg.:

var link= doc.querySelector('#courses_menu>ul>li:nth-child(2) a');

Now old-school DOM Level 0 gives you several easy properties to get the parts of a URL inside a link without having to resort to unreliable regex processing. If you want the whole query string crsEdId=78 , you can say:

var query= link.search.substring(1);

(the substring is to take off any leading ? character.)

If there might be more query parameters and you want to specifically find the one called crsEdId you'd have to process the query fully, eg.:

var crsEdId= getParameter(query, 'crsEdId');

function getParameter(query, name) {
    var pars= query.split(/[&;]/g);
    for (var i= pars.length; i-->0;) {
        var n= pars[i].split('=')[0];
        if (decodeURIComponent(n)===name)
            return decodeURIComponent(pars[i].substring(n.length+1));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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