


JavaScript operates XML using Baidu RSS as a news source example_javascript skills
js operates xml source, as the dynamic news of the page
, refer to the JS source code as follows (save as rss.js file):
var main = document.getElementById("content").getElementsByTagName("DIV");
/*
* There is a file named xml under the current directory subfolder of the directory in which the source referenced below is saved.
* The colon in each line below is preceded by the file name, followed by the xml source address (the xml file can be downloaded from the source address, and saved as the corresponding file name after downloading)
* You can right-click on the address below , select the target to save as, after downloading, you will get a txt file, just change the extension to xml
* movie:"http://news.baidu.com/n?cmd=1&class=film&tn=rss"
* woman:"http://news.baidu.com/n?cmd=1&class=healthnews&tn=rss"
* house:"http://news.baidu.com/n?cmd=1&class=housenews&tn= rss"
* car:"http://news.baidu.com/n?cmd=1&class=autonews&tn=rss"
* sport:"http://news.baidu.com/n?cmd= 1&class=sportnews&tn=rss"
* edu:"http://news.baidu.com/n?cmd=1&class=edunews&tn=rss"
*/
var RssSource = {
movie: "xml/movie.xml",
woman: "xml/woman.xml",
house: "xml/house.xml",
car: "xml/car.xml",
sport: "xml/sport.xml",
edu: "xml/edu.xml"
}
function Init() {
LoadXml(RssSource.movie, main[0]);
LoadXml(RssSource.woman, main[1]);
LoadXml(RssSource.house, main[2]);
LoadXml(RssSource.car, main[3]);
LoadXml(RssSource .sport, main[4]);
LoadXml(RssSource.edu, main[5]);
}
function LoadXml(url, target) {
var xml = null;
var isIE = true;
if (window.ActiveXObject) //IF IE
{
xml = new ActiveXObject("Microsoft.XMLDOM");
isIE = true;
} else if (document.implementation.createDocument) //IF FF
{
xml = document.implementation.createDocument("", "", null);
isIE = false;
}
xml .async = false;
xml.load(url);
//Get the XML document root node
var root = xml.documentElement;
//Get the item node in the RSS XML source
var items = root.getElementsByTagName("item");
//Create DOm object - RSS title
var head = document.createElement("dt");
head.setAttribute("style ", "background-color:#ccc;cursor:pointer;");
if (isIE) { //Operation when IE
head.innerHTML = "" (root.getElementsByTagName("title ")[0].text).substring(2, 6) " " root.getElementsByTagName("pubDate")[0].text "";
target.appendChild(head);
//Create DOm object - RSS list
var ul = document.createElement("ul");
//Add the list to the DIV container
target .appendChild(ul);
//Loop to output daily news to li, where items.length is the number of news items
for (i = 0; i < items.length; i ) {
/ /Create DOM object li to store news
var li = document.createElement("li");
//Create DOM hyperlink object
var lk = document.createElement("a");
//Time
//The title attribute of the hyperlink is also used to save the news text
lk.title = items[i].selectSingleNode("title").text;
//Set the hyperlink The href attribute
lk.href = items[i].selectSingleNode("link").text;
//The text displayed by the hyperlink, if it is longer than 15 characters, is intercepted and then added...
lk.innerHTML = lk.title.length > 18 ? lk.title.substring(0, 16) "....": lk.title;
//lk.innerText = lk.title;
//Add li to ul
ul.appendChild(li);
//Add hyperlink to li
li.appendChild(lk);
}
} else { //Operation when not IE
head.innerHTML = "" (root.getElementsByTagName("title")[0].textContent).substring(2, 6) " " root.getElementsByTagName("pubDate")[0].textContent "";
target.appendChild(head);
//Create DOm object - RSS list
var ul = document.createElement("ul");
//Add the list to the DIV container
target.appendChild(ul);
//Loop to output daily news to li, where items .length is the number of news items
for (i = 0; i < items.length; i ) {
//Create DOM object li to store news
var li = document.createElement("li" );
//Create DOM hyperlink object
var lk = document.createElement("a");
//Time
//The title attribute of the hyperlink is also used to save the news text
lk.title = items[i].getElementsByTagName("title")[0].textContent;
//Set the href attribute of the hyperlink
lk.href = items[i].getElementsByTagName(" link")[0].textContent;
//The text displayed by the hyperlink, if it is longer than 15 characters, intercept it and add...
lk.innerHTML = lk.title.length > 18 ?lk.title.substring(0, 16) "....": lk.title;
//lk.innerText = lk.title;
//Add li to ul
ul .appendChild(li);
//Add hyperlink to li
li.appendChild(lk);
}
}
}
显示页面参考源码(存为htm页面)
CSS源码(存为index.css )
a:link,a:visited,a:active {
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
#pagebody {
margin:0 auto;
width:800px;
height:1200px;
border-left:dotted 1px gray;
border-right:dotted 1px gray;
background-color:#eee;
}
#header {
height:200px;
}
#banner {
height:160px;
background-color:#fff;
}
#content div {
width:380px;
height:270px;
border:solid 1px gray;
overflow:hidden;
background-color:#fff;
}
#content div ul li {
list-style-image:url(list.gif);
}
.left {
float:left;
margin-top:10px;
margin-left:10px;
}
.right {
float:right;
margin-top:10px;
margin-right:10px;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
