顯示具有 Jquery 標籤的文章。 顯示所有文章
顯示具有 Jquery 標籤的文章。 顯示所有文章

2021年9月1日 星期三

Views

tr 顯示或隱藏

 解決問題:如果要用前端控制tr的顯示或隱藏

  引用自下文參考,擷取文字記錄

  

方法1.
   //隱藏後頁面位置被占用,只是不顯示
	document.getElementById("id").style.visibility="hidden";
        document.getElementById("id").style.visibility="visible";
   實驗
   <table>
   	<tr id="test" style="visibility:hidden">
            <th>名稱1</th>
            <td>小明</td>
        </tr>
        <tr >
            <th>名稱2</th>
            <td>小楷</td>
        </tr>
   </table>
方法2.
	//隱藏後頁面位置被占用,只是不顯示,隱藏後顯示樣式會跑掉,會改變原有style,樣式會被覆蓋掉
=       
	document.getElementById("id").style.display="none";
        document.getElementById("id").style.display="inline";
    <table>
   	<tr id="test" style="display:none">
            <th>名稱1</th>
            <td>小明</td>
        </tr>
        <tr >
            <th>名稱2</th>
            <td>小楷</td>
        </tr>
   </table>
方法3
	$('#id').show()/hide() 樣式不會跑掉
<table>
   	<tr id="test" style="display:none">
            <th>名稱1</th>
            <td>小明</td>
        </tr>
        <tr >
            <th>名稱2</th>
            <td>小楷</td>
        </tr>
   </table>
方法4 直接下hidden可以隱藏該欄位
   
<table>
   	<tr id="test" hidden>
            <th>名稱1</th>
            <td>小明</td>
        </tr>
        <tr >
            <th>名稱2</th>
            <td>小楷</td>
        </tr>
   </table>
參考文章

2021年6月13日 星期日

Views

JavaScript 和Jquery差異比較

 1.在區塊放入html元素字串 有<div id='cards'></div>

 js:

document.queryselect('#cards').innerHTML='<p>文字</p>';


jquery:

 $('#cards').html('<p>文字</p>');

2021年6月4日 星期五

Views

API 傳值和回傳

 目的:練習使用json.html和json.aspx發出ajax請求,並使用json.ashx(沒有畫面的api)返回json物件格式


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>

$.ajax各屬性代表意思
(1).請求ashx檔案的時候 要把contentType去掉,還有就是
data 格式為 {”key”,”value”};切記 不要再 大括號外面加雙引號,這樣就會在ashx頁面取不到資料而失敗。
(2).
contentType
contentType是網頁要送到Server的資料型態,若沒指定則預設為'application/x-www-form-urlencoded; charset=UTF-8'
dataType
dataType是網頁預期從Server接收的資料型態,若沒指定則jQuery會根據response的MIME type來推定為xml, json, script, html, text。

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).

2021年5月21日 星期五

Views

抓取jquery下拉選單值的問題

 前端:每次要給下拉選單selectedIndex,值給到但是顯示確未變

原因:因為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()方法只能抓到第一個子元素


2021年4月10日 星期六

Views

Jquery 函式庫用法

 1. animate() 動畫效果之意


一堆假文字 
  <div id="a"/></div>//隱藏錨點  
//搭配jquery
$('#mybtn').click(function(){   //當按鈕按下

    $("html,body").animate({   //html的body 動畫效果 就會搭配網頁卷軸效果 $.attr(this, 'position') 抓到該顆按鈕的position屬性
    scrollTop:$($.attr(this, 'position')).offset().top},1000); //scrollTop 控制網頁卷軸 1000 是動畫移動速度毫秒
    });//然後網頁卷軸以1000毫秒偏移到設定錨點的a位置上方 

2020年7月12日 星期日

Views

比較JS和JQ的選擇器

<body>
     value="Get" />
    <input type="text" id="My" name="You" class="First"         
     <script src="all.js"></script>
</body>

1.透過Id取值
 var jsid=document.getElementById("My").value;
 var jqid=$('#My').val();

2.透過Name取值
var jsname=document.getElementsByName('You')[0].value;
//其中getElementsByName取得是NodeList沒有value屬性,要加上[0]
var jqname=$("*[name='You']").val();

3.透過Class取值
 var jsclass=document.querySelector('.First').value;
 var jqclass=$('.First').val();


4.參考好文1,參考好文2, 3.js的選擇器  4.jquery 選擇器

5.JavaScript Event事件整理
 <input type='button' id='mybtn' value='按钮'></input>
 <script>
        var btn=document.getElementById('mybtn');

            btn.onclick=function(e){
            console.log(this.id); //得到:mybtn
            console.log(e.target);//輸出點擊事件的按鈕
            let x=e.target;
            console.log(x.id);    //得到:mybtn
            console.log(x.type);  //得到:button
console.log(x.value); //得到:按鈕 console.log(e.currentTarget);//輸出點擊事件所在對象 } </script>

2020年6月16日 星期二

Views

ASP.NET 搭配Jquery

以WebForm設計模式

1.<asp:Button id="btn" Text="Click" runat="server"/>
如果要用Jquery抓取id

console.log('#'+'<%= btn.ClientID%>')

2.前端<asp:Literal id="Literal" runat="server"/>
  後端Literal.Text="<div id='name'>Hello</div>";
  如果要用Jquery抓取<div>內容,並用console印出來,是html()


 <script>
     $(function(){
         var div=$("#name").html();
         console.log(div);
     });
 </script>
  心得:可由後端組出html字串,再由前端寫jquery去操作!

2020年5月30日 星期六

Views

在Boothstraip 4 下引用fancybox

1.Boothstraip4版本 4.0.0 Jquery-3.3.1.min.js

   Boothstraip和fancybox 需引用檔案及順序,在head引用
<link rel="stylesheet" href="Boothstraip4/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="basicfancybox/jquery.fancybox.css">
 
然後在</body>結尾上面引用下面連結檔案,檔案在自行上網下載
<script src="basicfancybox/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="basicfancybox/jquery.fancybox.js" type="text/javascript"></script>
<script src="basicfancybox/fancybox-setting.js" type="text/javascript"></script>
<script src="Boothstraip4/bootstrap.min.js">
至於fancybox-setting.js 是當畫面一開始時找到給與fancybox的超連結,如:
$(document).ready(function(){
    $("#123").fancybox({  //#123 表示畫面有一個<a id="123" href="Hello.html"/>
        'overlayColor': '#000',
        'padding': 0,
        'width': 1000,
        'autoSize': true,
        'transitionIn': 'easingIn',
        'transitionOut': 'easingOut',
        'type': 'iframe'           
    });       
});
詳細屬性介紹可以參考,不在贅述。

2.畫面中<a id="123" href="Hello.html">ParentValue</a>

   其中href是彈跳視窗內頁的程式,可以是html和aspx檔案(但aspx檔案都 要伴隨aspx.cs檔案)

3.母頁傳值到子頁接值

   記得在子頁(Hello.html)要引用<script src="basicfancybox/jquery-3.3.1.min.js"         type="text/javascript"></script>其中接值寫法

<body>
This Is FancyBox Page
<p>The id for this column is: <span id="nameid"></span></p>
<script type="text/javascript">
$(document).ready(function(){$("#nameid").text(parent.document.getElementById('123').innerText); }); </script> </body> 
4.子頁傳值到母頁並關閉
   在子頁寫上這段程式<input type="text" id="child"/>
<button type="submit" id="btn">送出</button>
<script type="text/javascript">
    $(document).ready(function(){
        $('#btn').click(function(){          
            parent.$.fancybox.close(); 關掉fancybox
            parent.GetVal($('#child').val()); 抓母頁GetVal方法,並傳id為child值過去
        });
    });
</script>
   在母頁寫上<div id="parent"></div>
<script>
function GetVal(val)
{
  $('#parent').text(val);//在這個地方就可以賦予id為parent的值了
}
</script>
大功告成嚕!!!!!

2020年5月14日 星期四

Views

[JS JQUERY]使用心得

1.可以在任何標籤加上元素

<lable id='My' data-id='1' >您好</lable>

取得方式:
alert($("#My").data("id"));  得值為1

2.可以使用選擇多個id同時添加想要的元素

        <lable id='My1' data-id='1' >您好</lable>
        <lable id='My2'>我好</lable>
        <lable id='My3'>誰好好</lable>

添加方式:
         $(document).ready(function(){
        
                 $("#My1,#My2,#My3").append('<input id type=\'text\'/>');
       
             });

3.How to trigger a click on a link using jQuery

<ul id="tit" class="gallery">
     <li>
    <a href="#inline" rel="prettyPhoto">Talent</a>
     </li>
</ul>
         $('#tit').find('a').trigger('click');
          $('#tit a').trigger('click');