使用ajax显示文章内容
接到一个东西,
要求:
一个页面上面有4个层,每层显示一篇新闻内容,
由于使用cms开发,不太灵活,没想到什么办法。
老大说用ajax查这4篇文章的标题。
想请教我怎么用ajax显示出这4篇文章内容。。。
不是很懂 , 求教中。。。
[解决办法]
http://www.cnblogs.com/eecc/archive/2008/07/29/1255389.html
[解决办法]
是不是点击这四个标题分别显示相应的文章内容?
那你在文章标题的标签上写上相应的文章ID的属性,点击标题之后将自己的文章ID发送的服务器上,返回文章的内容,然后用html插入的方式替代掉。
代码参考:
<script type="text/javascript"> $(function () { $(".article").click(function () { var articleid = $(this).attr("articleid"); $.ajax({ url: "xxx.ashx",//你相应路径的ashx页面,接收文章ID参数,返回文章内容html 如果是别人cms你看看它有没有给相应的接口或者方法 type: "post|get",//传值方式 post或者get均可 data: { "articleid": articleid },//你传过去的文章ID success: function (result, status) {//回调函数 if (result != "") { $(".content").html(result);//异步更新掉文章内容 } } }) }) }) </script>
[解决办法]
html代码
<div class="tbl-box">
<div id="tftable">
</div>
</div>
<div style="display:none;" class="pagelist" style="margin-top: 6px;">
<span id="spanPageinfo1" ></span>
<p class="fl">
<span class="pre" style="cursor: hand" onclick="FirstPage()"></span><span style="cursor: hand" class="pre1" onclick="OnPage()" >
</span><span>共<span id="spanAllRowS"></span>行 第<span id="spanPageIndex1"></span>页 第 <span
id="spanStartRows"></span>-<span id="spanEndRows"></span> 行 </span><span style="cursor: hand"
class="next" onclick="NextPage()" ></span><span style="cursor: hand" class="next1" onclick="LastPage()"></span></p>
</div>
<script >
gettableinfor(1);
var pageIndex = 1;
function gettableinfor(pageto)
{
$("#add_user").attr("disabled", "");
$("#a1").attr("disabled", "");
if('<%=accountids.ToString() %>'!=""){
$("#tftable").html("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='tbl-Ttop-border'><tr><td colspan='16' style='text-align:center;'>获取数据中!</td></tr></table>");
var txtboxe=$("#txtsele").val();
$("#spanStartRows").html("1"); //开始行
$("#spanEndRows").html("1"); //结束行
$("#spanPageIndex1").html("1"); //当前页
var pagesize=20;
if(pageto==0){
pageIndex = 1;
}else
{
pageIndex=pageto;
}
var text=Web3._0.PreventMaliciousclicks.pmcIPRuledOut.Get_Infor_table('<%=accountids.ToString() %>',txtboxe,pagesize,pageIndex);
var infos = text.value.split("$$$");
count=Number(infos[1]);
if(pageto == 1)
{
$("#spanStartRows").html(1); //开始行
if(Number(infos[1])<=20){
$("#spanEndRows").html(infos[1]); //结束行
}else
{
$("#spanEndRows").html(20); //结束行
}
} else {
$("#spanStartRows").html(20 * (pageto - 1) + 1); //开始行
$("#spanEndRows").html((20 * pageto)<=Number(infos[1])?(20 * pageto):Number(infos[1])); //结束行
//Number(infos[1]);
}
$("#tftable").html(infos[0]);
$("#spanAllRowS").html(infos[1]); //总共行数
$("#spanPageIndex1").html(pageto); //当前页
}else
{
$("#add_user").attr("disabled", "disabled");
$("#a1").attr("disabled", "disabled");
$("#tftable").html("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='tbl-Ttop-border'><tr><td colspan='16' style='text-align:center;'>请先绑定用户!</td></tr></table>");
return ;
}
}
function FirstPage() {
var allDataCount = $("#spanAllRowS").html();
if (allDataCount > 0) {
if (pageIndex != 1) {
pageIndex = 1;
gettableinfor(pageIndex)
}
}
}
//尾页
function LastPage() {
var allDataCount = $("#spanAllRowS").html(); //总行
if (allDataCount > 0) {
if (allDataCount % 20 == 0) {
pageIndex = allDataCount / 20;
} else {
pageIndex = parseInt(allDataCount / 20) + 1;
}
if (pageIndex > 1) {
gettableinfor(pageIndex)
}
}
}
function NextPage() {
var allDataCount = $("#spanAllRowS").html(); //总行
var allPageCount = 1;
if (allDataCount > 0) {
if (allDataCount % 20 == 0) {
allPageCount = allDataCount / 20;
} else {
allPageCount = parseInt(allDataCount / 20) + 1;
}
if (allPageCount > 1 && allPageCount > pageIndex) {
pageIndex = pageIndex + 1;
gettableinfor(pageIndex)
} } }
function OnPage() {
var allDataCount = $("#spanAllRowS").html(); //总行
var allPageCount = 1; //总页数
if (allDataCount > 0) {
if (allDataCount % 20 == 0) {
allPageCount = allDataCount / 20;
} else {
allPageCount = parseInt(allDataCount / 20) + 1;
}
if (allPageCount > 1 && allPageCount >= pageIndex && pageIndex>1) {
pageIndex = pageIndex- 1;
gettableinfor(pageIndex)
}
}
}
function pageToPage(indexPage) {
//当前页必须>0
if (indexPage >= 0) {
gettableinfor(pageIndex)
}
}
</script>
后面的代码
执行sql语句代码,
/// <summary>
/// 获取百度账户IP清除信息表
/// </summary>
/// <param name="account">用户编号</param>
/// <returns></returns>
public static DataSet Get_white(string account,out int count, string txt,int pageSize, int pageIndex)
{
string sqlstr = "select * from _infor where ";
string txtvlue = "";
if (txt!="" && txt!=null)
{
txtvlue = " ip='" + txt.Trim().ToString() + "' and";
}
sqlstr += txtvlue + " accountid in (" + account + ") order by id desc , id limit " + pageSize + " offset " + (pageIndex - 1) * pageSize;
string sqlcount = "select count(*) from _infor where "+ txtvlue +" accountid in(" + account + ")";
DataSet ds=GetDataMySQL.GetDataSet(sqlstr, sqlcount);
count =Convert.ToInt32( ds.Tables[1].Rows[0][0]);
return ds;
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="userid">用户id</param>
/// <param name="seltext">输入的查询条件ip</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Get_Infor_table(string userid, string seltext, int pageIndex, int pSize)
{
int count = 0;
userid = userid.Trim(',');
DataSet ds = DBkeyword.Get_white(userid, out count,seltext, pageIndex, pSize);
if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
{
return "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='tbl-Ttop-border'><tr><td colspan='16' style='text-align:center;'>暂无数据!</td></tr></table>" + "$$$" + count; ;
}
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='new-tbl-b'>");
sb.Append("<tr><th width='2%'></th><th width='6%'><a href='#' class='btn-defalut'><span id='fanxuan' style='font-weight: bold;display:none' onclick='Get_quanxuan(1)' >全不选</span><span id='rows' onclick='Get_quanxuan(0)' style='font-weight: bold;'>全 选</span></a>");
sb.Append("</th><th width='6%'>账户名称</th><th width='21%'>IP</th><th width='27%'>状 态</th></tr>");
DataSet set = new DataSet();
int icous = 1;
foreach (DataRow dt in ds.Tables[0].Rows)
{
string accouidname = "";
if (dt["accountid"].ToString()!="")
{
set= DBkeyword.Get_white(Int32.Parse(dt["accountid"].ToString()));
}
if (set.Tables[0].Rows[0]["param2"].ToString() != "" && set.Tables[0].Rows[0]["param2"] != null)
{
accouidname = set.Tables[0].Rows[0]["param2"].ToString();
}
else
{
if (set.Tables[0].Rows[0]["username"].ToString() != "" && set.Tables[0].Rows[0]["username"] != null)
{
accouidname = set.Tables[0].Rows[0]["username"].ToString();
}
}
sb.Append("<tr><td>").Append(icous).Append("</td>");
sb.Append("<td>"); sb.Append("<input type='checkbox' id='cbkey").Append("' name='cbKeywords' ipcon='").Append(dt["accountid"].ToString()).Append("' roleid='").Append(dt["id"]).Append("' />");
sb.Append(" </td><td>").Append(accouidname).Append("</td><td width='27%'>"); sb.Append(dt["ip"].ToString());
sb.Append("</td> <td width='21%'>"); sb.Append("该用户IP不会显示你的广告");
sb.Append(" </td></tr>");
icous++;
}
sb.Append("</table>");
return sb.ToString()+"$$$"+count;
}
这个代码完全可以实现,比葫芦画瓢.以上代码,完全安全.右击查看源码,别人几乎看不到你写的代码.不信你试试.