---
# System prepended metadata

title: '[jquery]jquery筆記'
tags: [jquery]

---

# [jquery]jquery筆記
###### tags: `jquery`

## 其他
### 判定jquery有無引用
```javascript=
if (typeof jQuery !== "undefined") {
    console.log("jQuery version:", jQuery.fn.jquery);
} else {
    console.log("jQuery is not loaded.");
}
```


## DOM
### 重設表單內容
https://www.tutorialrepublic.com/faq/how-to-reset-a-form-using-jquery.php

```javascript=
$("#form1").trigger("reset");
```

## 事件相關

### 自訂enter事件
https://stackoverflow.com/questions/6524288/jquery-event-for-user-pressing-enter-in-a-textbox
```javascript=
$.fn.pressEnter = function(fn) {  

    return this.each(function() {  
        $(this).bind('enterPress', fn);
        $(this).keyup(function(e){
            if(e.keyCode == 13)
            {
              $(this).trigger("enterPress");
            }
        })
    });  
 }; 

//use it:
$('textarea').pressEnter(function(){alert('here')})
```


### 將element的事件移除:使用off()
https://www.w3schools.com/jquery/event_off.asp
>unbind()已棄用

```javascript=
$('p').off();
```
>使用情境:
>假設某元素有被全域綁定某事件,
>不想使用該綁定事件,
>可先用off()將全域事件解除綁定,
>再綁定新的事件


## ajax
https://dotblogs.com.tw/hank_blog/2018/01/30/121006

```javascript=
$.ajax({
	 url : targetFewSourceCodeUrl,
	 method : "GET"
})
.done(SuccessMethod)
.fail(FailMethod);

```

## input
https://gist.github.com/yaqxyaqx/6227595
```javascript=

```

## remove disabled attribute
https://stackoverflow.com/questions/13626517/how-to-remove-disabled-attribute-using-jquery
```javascript=
$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
})
```

## multiple event,same action
https://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function
```javascript=
$('#element').on('keyup keypress blur change', function(e) {
    // e.type is the type of event fired
});
```

## reload page
https://stackoverflow.com/questions/5404839/how-can-i-refresh-a-page-with-jquery
```javascript=
$('#something').click(function() {
    location.reload();
});
```

## window.history.back和window.history.back的區別
https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/292411/



