简体   繁体   English

使用Javascript无效工作将内容附加到div

[英]appending contents to a div using Javascript not working

I have a Javascript code for decryption, now i want to display the decryption string inside a div. 我有一个用于解密的Javascript代码,现在我想在div中显示解密字符串。 I have tried using the following code. 我尝试使用以下代码。

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>

<script>
    var decrypted = CryptoJS.AES.decrypt("dsfsdffd", "dsfsdf");
    var dec = decrypted.toString(CryptoJS.enc.Utf8);

    document.getElementById("display1").innerHTML = dec;
</script>

<div id="display1"></div>

But the above code not working. 但上面的代码不起作用。 What i have done wrong. 我做错了什么。 Is there any solution. 有什么解决方案吗?

That's because your script is executed before the div is added to DOM. 那是因为在将div添加到DOM之前执行了脚本。 Move your script below the div, best before </body> tag. 将您的脚本移到div下面,最好在</body>标记之前。

Since you have tagged the question with jQuery, here is the jQuery solution 既然你用jQuery标记了这个问题,那么这就是jQuery解决方案

 $(document).ready(function () {
   var decrypted = CryptoJS.AES.decrypt("dsfsdffd", "dsfsdf");
   var dec = decrypted.toString(CryptoJS.enc.Utf8);
   $("#display1").html(dec);
 });

Here you can place your script before the elements. 在这里,您可以将脚本放在元素之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM