是否可以在此表单中使用每个循环的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;
});
});
});
答案 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
匹配以值
开头和结尾的链接