2019年2月25日 星期一

jquery 1.X Function

Online edit: https://playcode.io/online-javascript-editor

0. JS syntax

Variable

declare:  var  message = ""; 


Array: 

var data1 = {"id":1, "system": "abc", "name": "andy"}
data1.length




Exception:


  try { 
    if(x == "")  throw "empty";
    if(isNaN(x)) throw "not a number";
    x = Number(x);
    if(x < 5)  throw "too low";
    if(x > 10)   throw "too high";
  }
  catch(err) {
    message.innerHTML = "Input is " + err;
  }

1. How to post data 

$.ajax({
 type: 'POST',
 url: url,
 data: data,
 dataType: dataType,
 success: function(response) {...},
 error: function(jqXHR, textStatus, errorThrown) {...},
});

  $.post("demo_test_post.asp",
    {
      name: "Donald Duck111",
      city: "Duckburg"
    },
    function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    });
  });



ref 1 2




2. Get/Set Form data

function: $("#id").val("contnet")




3. Parse Json

1. Simple parse
var parsedJson = $.parseJSON(jsonToBeParsed);
ref 1

2. Check string is json

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

4.  Button Cancel / close

https://stackoverflow.com/questions/6445946/display-yes-and-no-buttons-instead-of-ok-and-cancel-in-confirm-box


5.  Div auto 

1<div style="width:300px;height:250px;overflow:auto;">
2您要輸入的內容
3</div>





6.  Replace the word

function convert(str)
{
  str = str.replace(/&/g, "&amp;");
  str = str.replace(/>/g, "&gt;");
  str = str.replace(/</g, "&lt;");
  str = str.replace(/"/g, "&quot;");
  str = str.replace(/'/g, "&#039;");
  return str;
}

7.  Jquery Add and remove class

1
$( "p" ).removeClass( "myClass noClass" ).addClass( "yourClass" );


ref 1


8. JS undefined check

if (typeof(myVariable) != "undefined")
Or
if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?
ref 1


9.  How to set number

var nf = new Intl.NumberFormat();
nf.format(number); // "1,234,567,890"

11. Set object

1

var object = {
  foo: 1,
  "bar": "some string",
  baz: {
  }
};


12.  function
1


13.  javascript regular

console.log(/^.+\/\d+,.+$/.test('1abc/24,abc'))
var myString = "something format_abc";
var arr = myString.match(/\bformat_(.*?)\b/);
console.log(arr[0] + " " + arr[1]);
ref 1 2

沒有留言:

張貼留言