jquery循环和比较值

时间:2012-06-19 09:11:32

标签: jquery

是否可以在此表单中使用每个循环的jqueries来分解超链接?

 $(document).ready(function() {
    var toExclude =['http://www.google.com', 'http://example'];
    $.each(toExclude, function(key, value) {

    $("a[href='+value+']").replaceWith(function(){
        var matches = value.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
        var domain = matches && matches[1];
        return domain;
                });
            });
        });

3 个答案:

答案 0 :(得分:5)

$("a[href='+value+']")

应该是:

$("a[href='"+value+"']")

此外,您可能只想更改href属性=>使用.attr方法:

$("a[href='"+value+"']").attr("href", function() {
    // do your replace and return the new value of the href
    ...
});

答案 1 :(得分:1)

var toExclude = ['www.goo.com','www.dgoo.com'];

$.each(toExclude, function(i,e)
       {

           $("a[href^='"+e+"']").each(function (i,e) {$(e).replaceWith($(e).attr('href'));});
       }
​);
       ​

答案 2 :(得分:0)

$('a[href="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

报价标志稍有变化..这也允许你做

$('a[href$="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

$('a[href^="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

匹配以

开头和结尾的链接