1.在區塊放入html元素字串 有<div id='cards'></div>
js:
document.queryselect('#cards').innerHTML='<p>文字</p>';
jquery:
$('#cards').html('<p>文字</p>');
1.在區塊放入html元素字串 有<div id='cards'></div>
js:
document.queryselect('#cards').innerHTML='<p>文字</p>';
jquery:
$('#cards').html('<p>文字</p>');
1.json.html語法
<!DOCTYPE html>
<head >
<title>JSON</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<H5>Hwllo World</H5>
<script>
$.ajax({
url: "json.ashx",
data: {"id":"0125","name":"Tina"},
type: "POST",
dataType: "json",
success: function(returnData){
console.log(returnData);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
}
});
</script>
</body>
</html>
2.json.aspx語法code behind要有json.aspx.cs就不放上
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Template.aspx.cs" Inherits="Template" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Ch2</title>
<title>Bootstrap Navigation Bar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body >
<h1>This is Empty</h1>
<form runat="server">
</form>
<script>
$.ajax({
url: "json.ashx",
data: {"id":"0125","name":"Tina"},
type: "POST",
dataType: "json",
success: function(returnData){
console.log(returnData);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
}
});
</script>
</body>
</html>
3.調用json.ashx的API
<%@ WebHandler Language = "C#" Class = "ResponseOds" %> using System; using System.Web; using System.Collections.Generic; using System.IO; using System.Web.Script.Serialization; using Newtonsoft.Json; public class ResponseOds : IHttpHandler { public void ProcessRequest (HttpContext context) { string id=context.Request["id"]; //接到前端傳來值 if(id=="0125") //判斷是0125的值,在將相對應要回傳值丟到前台 { List<Student> lstStuModel = new List<Student>() { new Student(){ID=1,Name="張飛",Age=250,Sex="男"}, new Student(){ID=2,Name="潘金蓮",Age=300,Sex="女"} }; //Newtonsoft.Json序列化 string jsonData = JsonConvert.SerializeObject(lstStuModel); context.Response.ContentType = "application/json"; //回傳json格式 context.Response.Charset = "utf-8"; context.Response.Write(jsonData); } } public bool IsReusable { get { return false; } } class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } }參考資料 (1).(2).(3).
原因:因為Boothtriap4 的顯示內容沒有更改到
解法:只要抓到顯示內容地方修改後就可以。
程式碼:
<select id="Num"> <option>請選擇數字</option> <option>1</option> <option>2</option> </select> <button> <div class="filter-option"> <div class="filter-option-inner"> <div class="filter-option-inner-inner"> 取餐囉 </div> </div> </div> </button> let CityName=$(this).children(":first").data('info'); //取到目前抓取的jQuery物件,它子元素的第一個data-info的值 $("#Num")[0].selectedIndex =parseInt($('#CityIndex').val()); // 給予下拉選單選取值 $('#Num').next().children().children().children().text(CityName); //最後抓取下拉旁邊的按鈕它的子元素子元素子元素內容進行修改 //想到更簡潔的寫法為利用find的方法,到按鈕內遍歷它的子元素,找到有div class="filter-option-inner-inner" DOM物件 $('#Num').next().find('div .filter-option-inner-inner').text('找到了') //不同children()方法只能抓到第一個子元素