2016-11-11 71 views

回答

1

我建议to not use document.write

如果我理解你,使用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); 
+0

不工作:/,尝试它,请[这里](http://www.w3schools.com/jquery/tryit.asp?filename=F0O577DN2ZOO) –

+0

试图运行它时,我在get_gravatar函数内部发生错误:http://www.w3schools.com/code/tryit.asp?filename=F0O5N1T083IP –

+0

Omg,它工作正常! ,非常感谢我的朋友:) –

0

首先,您需要获取文本并将其存储在变量中,然后才能使用该文本。

您可以使用以下方法:

var email = document.getElementById('email'); 
document.write('<img src="' + get_gravatar(email, 150) + '" />'); 
0

使用.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) + '" />'); 
0
document.write('<img src="' + get_gravatar(document.getElementById("email").innerHTML, 150) + '" />'); 

JQuery的

​​
0

希望这有助于你,

var mail = document.getElementById('email').innerHTML; 
document.write('<img src="' + get_gravatar(mail, 150) + '" />');