English 中文(简体)
为什么它给我 TypeError: 无法读取未定义的属性( 读子子)?
原标题:why is it giving me TypeError: Cannot read properties of undefined (reading appendChild )

trying to create a div element after form is submitted to store input on next page must be able to create a new div for every post and render them, it only works when appended to body, i need it in the main

我尝试过多个谷歌修正, 并问一个朋友谁代码的标语

document.addEventListener( DOMContentLoaded , function() {
    var formData = JSON.parse(localStorage.getItem( formData ));

    if (formData) {
        var blogPostDiv = document.createElement( div );
        blogPostDiv.classList.add( blog-post );

        var usernamePara = document.createElement( p );
        usernamePara.textContent =  Author:   + formData.username;

        var titlePara = document.createElement( h2 );
        titlePara.textContent = formData.title;

        var contentPara = document.createElement( p );
        contentPara.textContent = formData.content;

        blogPostDiv.appendChild(usernamePara);
        blogPostDiv.appendChild(titlePara);
        blogPostDiv.appendChild(contentPara);
        
        // Append the div to the body or another desired location
        document.main.appendChild(blogPostDiv); 
    } else {
        // Handle case where data is not found in localStorage
        console.log( No blog post data found. );

    }
});
最佳回答

如果您在谈论元素 , 您需要从文档( DOM) 中获取元素, 才能在文档中附加任何内容 :

const mainElement = document.querySelector( main ) // Or document.getElementsByTagName( main )[0]
mainElement.appendChild(blogPostDiv); 

main 不是一个有效的文档 obj (默认) 属性

<强>Docs:

问题回答

You need to specify the element to add blogPostData. Please try this document.getElementById("your other id").appendChild(blogPostDiv);





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签