我试图让#email div的内容insdie我的javascript代码。使用jquery或任何方式获取div内容(javascript代码)
例子:
<div id="email">[email protected]</div>
我想打印在这里
电子邮件地址document.write('<img src="' + get_gravatar('Email Here', 150) + '" />');
非常感谢您!
我试图让#email div的内容insdie我的javascript代码。使用jquery或任何方式获取div内容(javascript代码)
例子:
<div id="email">[email protected]</div>
我想打印在这里
电子邮件地址document.write('<img src="' + get_gravatar('Email Here', 150) + '" />');
非常感谢您!
如果我理解你,使用jQuery:
(function($) //IIEF format
{
$(function() //wait for doc ready
{
var $img = $('<img></img>'); //create img tag using jquery
var email = $('#email').text().trim() //could also be .html() to get inner html
$img.attr('src', get_gravatar(email, 150)) //add email to src attribute of img tag
$('body').append($img); //change 'body' with whatever you want to add it to; this will append the img tag into the document
}
})(jQuery);
首先,您需要获取文本并将其存储在变量中,然后才能使用该文本。
您可以使用以下方法:
var email = document.getElementById('email');
document.write('<img src="' + get_gravatar(email, 150) + '" />');
使用.innerHTML
或.textContent
在div电子邮件
innerHTML属性设置的JavaScript或返回元素的HTML内容(内部HTML) 。
var emailId = document.getElementById('email').innerHTML;
document.write('<img src="' + get_gravatar(emailId, 150) + '" />');
或
如果元素是一个文件的textContent返回null,文档 型或符号。要获取整个文档的所有文本和CDATA数据,可以使用document.documentElement.textContent。
var emailId = document.getElementById('email').textContent;
document.write('<img src="' + get_gravatar(emailId, 150) + '" />');
document.write('<img src="' + get_gravatar(document.getElementById("email").innerHTML, 150) + '" />');
JQuery的
希望这有助于你,
var mail = document.getElementById('email').innerHTML;
document.write('<img src="' + get_gravatar(mail, 150) + '" />');
不工作:/,尝试它,请[这里](http://www.w3schools.com/jquery/tryit.asp?filename=F0O577DN2ZOO) –
试图运行它时,我在get_gravatar函数内部发生错误:http://www.w3schools.com/code/tryit.asp?filename=F0O5N1T083IP –
Omg,它工作正常! ,非常感谢我的朋友:) –