我有两个带有盐和密码的跨度
<div> $salt = '<span id="salt"></span>';<br /> $password = '<span id="password"></span>'; </div>
然后我使用以下jQuery代码获取salt,将其添加到用户输入的密码,并返回salt和散列密码(以复制到配置文件中)
salt = (Math.random() +1).toString(36).substr(2,16); $('#salt').html(salt); hashed = CryptoJS.MD5(salt + $(this).val()); $('#password').html(hashed);
此代码使用salt ID将salt添加到span中,但它不会向密码ID添加任何内容.
如果我将.html()更改为.text(),它就可以了.
解决方法
CryptoJS.MD5()方法返回一个对象.要将此对象转换为字符串,请使用toString()方法.
hashed = CryptoJS.MD5(value); var hashedString = hashed.toString();
而不是将文本插入元素使用jQuerys text()方法. jQuery html()方法用于设置元素innerHTML值.但是你想插入未转义的文字.
$('#password').text(hashedString);
如果一个对象传递给text(),它将自动调用它上面的toString(),尽管引用中没有记录
参考
.text(文字)
The text to set as the content of each matched element. When Number or Boolean is supplied,it will be converted to a String representation. […] be aware that this method escapes the string provided as necessary so that it will render correctly in HTML.
从jQuery API Documentation: text()
.html(htmlString)
A string of HTML to set as the content of each matched element