I need to remove the mailto:
from a javascript variable which containts the href attribute of an a element:
var email = $(this).find('a').attr('href');
The output should just be the email address. I try to append the address to a div element:
$(this).append(email);
I need to remove the mailto:
from a javascript variable which containts the href attribute of an a element:
var email = $(this).find('a').attr('href');
The output should just be the email address. I try to append the address to a div element:
$(this).append(email);
Share
Improve this question
edited Jan 12, 2017 at 9:11
Yaje
2,83119 silver badges32 bronze badges
asked Jan 12, 2017 at 9:05
public9nfpublic9nf
1,4093 gold badges20 silver badges52 bronze badges
1
-
Please use
$(this)
. – Mihai Alexandru-Ionut Commented Jan 12, 2017 at 9:05
7 Answers
Reset to default 5Simply remove the "mailto:" prefix from the string inside email
variable, using substring
method:
var emailRef = 'mailto:[email protected]';
// Get the substring starting from the 7th character to the end of the string
var email = emailRef.substring(7);
console.log(email);
var email = $(this).find('a').attr('href');
var address = email.split('mailto:')[1];
//Append to div
$('#divId').append(address);
You can split on the basis of :
present in href
var email = $(this).find('a').attr('href');
var onlyEmail = email.split(":")[1]
Just replace it. As simple as
(this).append(email.replace("mailto:","");
change this as:
var email = $(this).find('a').attr('href').split(':')[1]; // gives you email
Here .split(':')
will split the string at the index of :
and returns an array then you can take the [1]
first index to get the email.
For a demo:
var email = $('a').attr('href').split(':')[1];
console.log(email);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="mailto:[email protected]">Get the mail of this anchor.</a>
After all, it's just text:
"mailto:[email protected]?subject=Hi"
.replace(/^mailto:([^?]+).*/, '$1');
var email = $(this).find('a').attr('href');
$.each( email, function( key, value ) {
//check for mailto
if (value.startsWith('mailto:')){
//Append to div
$(this).append(address);
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745153443a4613971.html
评论列表(0条)