Javascript Ajax invokes news content and displays

Source: Internet
Author: User

HTML below, we have this page let Dedecms call the data News tab

The code is as follows Copy Code

Dedecms Template label is like

{dede:arclist typeid= ' 3 ' row= ' 6 ' orderby= ' ID '}
<li data-value= "url (/images/20131107153016.jpg)" >
<span class= "TXTBG" > </span>
<span class= "txt" ><a href= "javascript:;" onclick= "getnewscontent ([Field:id/])" >[field:title/]</a ></span>
</li>
{/dede:arclist}

After the HTML is generated, the following page

The code is as follows Copy Code


<li data-value= "url (/images/20131107153016.jpg)" >
<span class= "TXTBG" > </span>
<span class= "txt" ><a href= "javascript:;" onclick= "getnewscontent (1)" > Hi, I'm www.111cn.net</a></. Span>
</li>
<li data-value= "url (/images/20131107153016.jpg)" >
<span class= "TXTBG" > </span>
<span class= "txt" ><a href= "javascript:;" onclick= "Getnewscontent (2)" > Hi, I'm www.111cn.net</a></. Span>
</li>

In the above two we should note that there is a onclick= "getnewscontent (2)" which has a number, this is our news ID, now we look at the getnewscontent () this function

The code is as follows Copy Code

var xmlHttp
Function Getnewscontent (newsid)
{
 xmlhttp=getxmlhttpobject ()
 if (xmlhttp==null)
 {
   alert ("Browser does not support HTTP Request")
   return false;
 
&N Bsp
 var url= "/getnewscontent.php"
 url=url+ "? id=" +newsid
 url=url+ "&sid=" +Math.random ()
 xmlhttp.onreadystatechange=statechanged_n
 xmlhttp.open ("Get", url,true)
  Xmlhttp.send (null)
}

Function statechanged_n ()
{
 if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete")
 {
  var restext=xmlhttp.responsetext;
  document.getElementById (' News_scroll '). Innerhtml=restext;
  $ (' #news_detail '). Animate ({' Left ': 0}, ' Easeoutquint ');
 }
}

The code is very simple, we just use Ajax call getnewscontent.php, and then by getnewscontent.php return value after we put the value to news_scroll this div, added a dynamic effect $ (' #news_ Detail '). Animate ({' Left ': 0}, ' Easeoutquint '); This sentence is based on jquery, if you do not add may not have the effect of everyone or add.

getnewscontent.php

The code is as follows Copy Code

<?php
$id =isset ($_get[' id ')]? Intval ($_get[' id '): 0;
if ($id)
{
Include ("data/common.inc.php");
mysql_connect (' localhost ', $cfg _dbuser, $cfg _dbpwd) or Die (' Database connect Failer ');
mysql_select_db ($cfg _dbname);
mysql_query ("Set Names ' $cfg _db_language '");
$sql = "SELECT * from". $cfg _dbprefix ' addonarticle '. ' WHERE aid = '. $id;
$query = mysql_query ($sql) or Die ($sql);
if (mysql_num_rows ($query))
{
$rs = Mysql_fetch_array ($query);
echo $rs [' body '];
}
Else
{
Echo ' parameter error or record has been deleted! ';
}
}
Else
{
Echo ' parameter error or record has been deleted! ';
}
?>

Oh, come on, try it.

Note News_scroll This ID oh, such as <div id= "News_scroll" > This is the dynamic data to be displayed Oh </div> is also clicked on the news ID returned the value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.