Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

javascript - jQuery动态向div添加内容(jQuery dynamically add content to div)

What I'm trying to do is add for each items in a xml file their content to a html page.(我想做的是将xml文件中的每个项目的内容添加到html页面。)

I'm trying to make it so that every item content is inside an <article> tag.(我正在尝试使每个项目的内容都在<article>标记内。) However, It's my first time using jQuery and my end result is not what I want.(但是,这是我第一次使用jQuery,最终结果不是我想要的。) let $items = $(xmlContent).find("item"); $items.each(function() { let title = $(this).find('title').text(); let date = $(this).find('pubDate').text(); let link = $(this).find('link').text(); let guid = $(this).find('guid').text(); let photo = $(this).find('media').html(); $("#world").append("<article></article>", [$("<a>", { href: link }) + ("<h3>" + title + "</h3>")]); This is what the end result currently is:(这是当前的最终结果是:) <article></article> [object Object] <h3>Students Fainting From Hunger in Venezuela’s Failing School System</h3> And what I want it to become:(而我希望它成为:) <article> <a href= myLink <h3>Students Fainting From Hunger in Venezuela’s Failing School System</h3> </a> </article> I want to apply my link so that everywhere the user click on the content it goes to the link.(我想应用我的链接,以便到处用户单击链接的内容。) Hopefully you can guide me.(希望你能指导我。) Thanks!(谢谢!)   ask by Ron translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can build your article element step by step.(您可以逐步构建文章元素。)

Create 'article' as an element, then create your 'a' element.(创建“文章”作为元素,然后创建“ a”元素。) Append the 'h3' element to the 'a' element before appending 'a' to 'article', and then 'article' to '#world'.(在将“ a”附加到“ article”之后,再将“ article”附加到“ #world”之前,将“ h3”元素附加到“ a”元素。) let article = $("<article></article>") let a = $("<a></a>", {href: link}) a.append("<h3>" + title + "</h3>") article.append(a) $("#world").append(article)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...