这就是我想做的
我去了,但是好像在某处失败了!
任何人都可以露面吗?
谢谢
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>$(document).ready(function() {
function checkWidth() {
var windowSize = $(window).width();
if (windowSize < 1000) {
$('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">$1</a> ') ); });
} //make plain text url clickable
else if (windowSize < 1000) {
$('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="Link">Link</a> ') ); });
} //convert plain text to clickable word "Link"
}
checkWidth();
$(window).resize(checkWidth);
$(window).onload(checkWidth);
});
</script>
</head>
<body>
<div>https://www.google.com/</div>
<div>https://www.yahoo.com/</div>
</body>
</html>
答案 0 :(得分:1)
我认为这就是您想要的。
function checkWidth() {
var windowSize = $(window).width();
if (windowSize > 1000) {
$('div').each(function(){
var div = $(this).html();
var link = $(this).find('a');
var linkText = $(this).find('a').html();
if(link.length === 0) {
$(this).html(div.replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">$1</a> '));
} else {
$(this).find('a')[0].innerHTML = $(this).find('a')[0].href;
}
});
} //make plain text url clickable
else if (windowSize < 1000) {
$('div').each(function(){
var div = $(this).html();
var link = $(this).find('a');
var linkText = $(this).find('a').html();
if(link.length === 0) {
$(this).html(div.replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">Link</a> '));
} else {
$(this).find('a')[0].innerHTML = 'Link';
}
})
}//convert plain text to clickable word "Link"
}
$(document).ready(function() {
checkWidth();
});
//$(window).onload(checkWidth);
$(window).resize(function(){checkWidth();});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<div>https://www.google.com/</div>
<div>https://www.yahoo.com/</div>
</body>
</html>
答案 1 :(得分:0)
@jakecigar在jquery论坛中提供的好解决方案
HTML:
<div>https://www.google.com/</div>
<div>https://www.yahoo.com/</div>
CSS:
@media only screen and (max-width: 1000px) {
.abbr {
font-size:0;
text-decoration:none;
}
.abbr:after {
display: inline;
font-size:16px;
content: "link";
color:black;
}
}
JS:
$(function() {
$('div').each(function() {
$(this).html($(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a class=abbr href="$1">$1</a> '));
});
})