0

我正在尝试运行正则表达式来执行此操作:

替换文档中 apk 文件的所有实例:

<a href="http://download.mydomain.com/files/androidfile.apk">

和:

<a target="_blank" href="http://mydomain.com?f=http://download.mydomain.com/files/androidfile.apk">

使用此 JQUERY 代码:

$('body').contents().filter(function() {
    return this.nodeType === 3;
}).each(function() {
    $(this).replaceWith($(this).text().replace(/<a href~/^.*\.(apk)$/i, 'http://mydomain.com?f=">$1</a>'));
});

所以本质上,删除之前的代码download.mydomain.com/files/androidfile.apk,也就是<a href="http:// 替换为<a target="_blank" href="http://mydomain.com?f=http://. 一个页面中可能有几个这样的,所以我需要一个可以匹配并替换所有的解决方案。

正则表达式和替换似乎不起作用,这可能是一个解决方案。

所以总而言之,我希望能够在 JQuery 中做到这一点:

<html>

<a href="http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

</html>

对此:

<html>

<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

</html>
4

2 回答 2

1

The code below should do the trick. Rather than use regex, I used the ends with selector. This should serve the same purpose, and execute quicker. Instead of rewriting the tag completely, I just modified the attributes using jQuery.attr(). A live example is here.

<!DOCTYPE html>
<html>

<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    $('a[href$=apk]').each(function()
    {
        var mydomain = "http://mydomain2.com?f=";
        var $this = $(this);

        $this.attr("href", mydomain+$this.attr("href"));
        $this.attr("target", "_blank");
    });
});
</script>
</head>

<body>
<a href="http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>

</body>
</html>

The resulting output is:

<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
于 2012-12-14T19:22:29.537 回答
1

您正在做的是矫枉过正,虽然它不起作用,为什么要选择页面上的所有元素?如果您想选择a元素,为什么不使用选择它们$('a')?要向属性添加字符串href,可以使用attr方法。

$('a').filter(function() {
    return this.href.indexOf('apk')
}).attr('href', function(i, href) {
    return 'http://mydomain.com?f=' + href
}).attr('target', '_blank');

http://jsfiddle.net/NCC7u/

于 2012-12-14T19:17:44.813 回答