DOM、事件、表單與 jQuery

選取、修改、事件、表單,jQuery 與前端框架。

57 / 57 題 · 依票數排序

8.7k

How do I check if an element is hidden in jQuery?

How do I toggle the visibility of an element using .hide() , .show() , or .toggle() ? How do I test if an element is visible or hidden ?

2008javascriptjquerydomvisibilitydisplay·3 重點答案·3.3M 次瀏覽
7.7k

How do I redirect to another webpage?

How can I redirect the user from one page to another using jQuery or pure JavaScript?

2009javascriptjqueryhttp-redirect·3 重點答案·8.2M 次瀏覽
5.5k

How can I validate an email address in JavaScript?

I'd like to check if the user input is an email address in JavaScript, before sending it to a server or attempting to send an email to it, to prevent the most basic mistyping. How…

2008javascripthtmlregexemail-validation·3 重點答案·4.9M 次瀏覽
5.2k

How do I check whether a checkbox is checked in jQuery?

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery. For example, if the age checkbox is checked, then I need to sh…

2009javascriptjqueryhtmlcheckbox·3 重點答案·5M 次瀏覽
4.6k

Setting "checked" for a checkbox with jQuery

I'd like to do something like this to tick a checkbox using jQuery : $(".myCheckBox").checked(true); or $(".myCheckBox").selected(true); Does such a thing exist?

2009javascriptjquerycheckboxselectedchecked·3 重點答案·3.9M 次瀏覽
4.5k

"Thinking in AngularJS" if I have a jQuery background?

Suppose I'm familiar with developing client-side applications in jQuery , but now I'd like to start using AngularJS . Can you describe the paradigm shift that is necessary? Here a…

2013javascriptjqueryangularjs·3 重點答案·858k 次瀏覽
4.3k

How do I copy to the clipboard in JavaScript?

How do I copy text to the clipboard (multi-browser)? Related: How does Trello access the user's clipboard?

2008javascriptclipboardcopy-paste·3 重點答案·3.1M 次瀏覽
3.3k

event.preventDefault() vs. return false

When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to pl…

2009javascriptjqueryevent-handlingdom-eventsevent-propagation·3 重點答案·951k 次瀏覽
3.2k

Is it possible to apply CSS to half of a character?

What I am looking for: A way to style one HALF of a character. (In this case, half the letter being transparent) What I have currently searched for and tried (With no luck): Metho…

2014javascripthtmlcss·3 重點答案·301k 次瀏覽
3.2k

Is there an "exists" function for jQuery?

How can I check the existence of an element in jQuery? The current code that I have is this: if ($(selector).length > 0) { // Do something } Is there a more elegant way to approac…

2008javascriptjquery·3 重點答案·880k 次瀏覽
3.2k

How can I upload files asynchronously with jQuery?

I would like to upload a file asynchronously with jQuery. $(document).ready(function () { $("#uploadbutton").click(function () { var filename = $("#file").val(); $.ajax({ type: "P…

2008javascriptjqueryajaxasynchronousxmlhttprequest·3 重點答案·1.5M 次瀏覽
3.1k

How to store objects in HTML5 localStorage/sessionStorage

I'd like to store a JavaScript object in HTML5 localStorage , but my object is apparently being converted to a string. I can store and retrieve primitive JavaScript types and arra…

2010javascripthtmllocal-storage·3 重點答案·1.5M 次瀏覽
3.1k

How do I modify the URL without reloading the page?

Is there a way I can modify the URL of the current page without reloading the page? I would like to access the portion before the # hash if possible. I only need to change the por…

2009javascripthtmlurlurl-rewritingfriendly-url·3 重點答案·2.2M 次瀏覽
3.1k

How can I know which radio button is selected via jQuery?

I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery? I can get all of them like this: $("form :radio") How do I know which…

2009javascriptjqueryhtmljquery-selectorsradio-button·3 重點答案·2.6M 次瀏覽
3k

How do I detect a click outside an element?

I have some HTML menus, which I show completely when a user clicks on the head of these menus. I would like to hide these elements when the user clicks outside the menus' area. Is…

2008javascriptjqueryclick·3 重點答案·1.8M 次瀏覽
3k

Open a URL in a new tab (and not a new window)

I'm trying to open a URL in a new tab, as opposed to a popup window. I've seen related questions where the responses would look something like: window.open(url,'_blank'); window.o…

2011javascripttabshref·3 重點答案·4.1M 次瀏覽
2.7k

Scroll to an element with jQuery

I have this input element: <input type="text" class="textfield" value="" id="subject" name="subject"> Then I have some other elements, like other tag's & <textarea> tag's, etc...…

2011javascriptjqueryscroll·3 重點答案·3.2M 次瀏覽
2.7k

How can I determine if a variable is 'undefined' or 'null'?

How do I determine if a variable is undefined or null ? My code is as follows: var EmpName = $("div#esd-names div#name").attr('class'); if(EmpName == 'undefined'){ // DO SOMETHING…

2010javascriptjqueryvariablesnullundefined·3 重點答案·3.6M 次瀏覽
2.7k

Adding a table row in jQuery

I'm using jQuery to add an additional row to a table as the last row. I have done it this way: $('#myTable').append('<tr><td>my data</td><td>more data</td></tr>'); Are there limit…

2008javascriptjqueryhtml-table·3 重點答案·2.1M 次瀏覽
2.6k

Disable/enable an input with jQuery?

$input.disabled = true; or $input.disabled = "disabled"; Which is the standard way? And, conversely, how do you enable a disabled input?

2009javascriptjqueryhtml-inputdisabled-input·3 重點答案·2.7M 次瀏覽
2.5k

Remove duplicate values from a JavaScript array

I have a very simple JavaScript array that may or may not contain duplicates. var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]; I need to remove the duplicates an…

2012javascriptjqueryarraysduplicatesunique·3 重點答案·3.4M 次瀏覽
2.5k

.prop() vs .attr()

So jQuery 1.6 has the new function prop() . $(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style');…

2011javascriptjquerydomattrprop·3 重點答案·751k 次瀏覽
2.5k

Get the size of the screen, current web page and browser window

How can I get windowWidth , windowHeight , pageWidth , pageHeight , screenWidth , screenHeight , pageX , pageY , screenX , screenY which will work in all major browsers?

2010javascripthtmljquerylayoutcross-browser·3 重點答案·2.3M 次瀏覽
2.5k

$(document).ready equivalent without jQuery

I have a script that uses $(document).ready , but it doesn't use anything else from jQuery. I'd like to lighten it up by removing the jQuery dependency. How can I implement my own…

2009javascriptjquerypageload·3 重點答案·1.4M 次瀏覽
2.4k

Get selected value in dropdown list using JavaScript

How do I get the selected value from a dropdown list using JavaScript? <form> <select id="ddlViewBy"> <option value="1">test1</option> <option value="2" selected="selected">test2<…

2009javascripthtml-select·3 重點答案·6.1M 次瀏覽
2.4k

How to get the children of the $(this) selector?

I have a layout similar to this, and would like to use a jQuery selector to select the child img inside the div on click. <div id="..."> <img src="..."> </div> To get the div , I'…

2008javascriptjqueryjquery-selectors·3 重點答案·2M 次瀏覽
2.4k

How can I programmatically navigate using React Router?

With React Router , I can use the Link element to create links which are natively handled by React Router. I see internally it calls this.context.transitionTo(...) . I want to do…

2015javascriptreactjsreact-router·3 重點答案·1.8M 次瀏覽
2.2k

Preview an image before it is uploaded

I want to be able to preview a file (image) before it is uploaded. The preview action should be executed all in the browser without using Ajax to upload the image. How can I do th…

2010javascriptjqueryfile-upload·3 重點答案·1.9M 次瀏覽
2.1k

Loop inside React JSX

I'm trying to do something like the following in React JSX (where ObjectRow is a separate component): <tbody> for (var i=0; i < numrows; i++) { <ObjectRow/> } </tbody> I realize a…

2014javascriptreactjsjsx·3 重點答案·2.2M 次瀏覽
2.1k

Scroll to the top of the page using JavaScript?

How do I scroll to the top of the page using JavaScript? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.

2009javascriptscroll·3 重點答案·2.6M 次瀏覽
2k

How does data binding work in AngularJS?

How does data binding work in the AngularJS framework? I haven't found technical details on their site . It's more or less clear how it works when data is propagated from view to…

2012javascriptangularjsdata-binding·3 重點答案·366k 次瀏覽
2k

Abort Ajax requests using jQuery

Is it possible that using jQuery, I cancel/abort an Ajax request that I have not yet received the response from?

2009javascriptjqueryajax·3 重點答案·733k 次瀏覽
2k

How to detect a mobile device using jQuery

Is there a way to detect whether or not a user is using a mobile device in jQuery? Something similar to the CSS @media attribute? I would like to run a different script if the bro…

2010javascriptjquerymobilebrowser-detection·3 重點答案·2M 次瀏覽
2k

Where should I put <script> tags in HTML markup?

When embedding JavaScript in an HTML document, where is the proper place to put the <script> tags and included JavaScript? I seem to recall that you are not supposed to place thes…

2009javascripthtml·3 重點答案·915k 次瀏覽
1.9k

How to move an element into another element

I would like to move one DIV element inside another. For example, I want to move this (including all children): <div id="source"> ... </div> into this: <div id="destination"> ...…

2009javascriptjqueryhtml·3 重點答案·1.5M 次瀏覽
1.8k

Stop setInterval call in JavaScript

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event? I want the user to be able to stop the re…

2008javascriptdom-eventssetinterval·3 重點答案·1.5M 次瀏覽
1.8k

Convert form data to JavaScript object with jQuery

How do I convert all elements of my form to a JavaScript object? I'd like to have some way of automatically building a JavaScript object from my form, without having to loop over…

2009javascriptjqueryjsonserialization·3 重點答案·1.1M 次瀏覽
1.8k

JavaScript post request like a form submit

I'm trying to direct a browser to a different page. If I wanted a GET request, I might say document.location.href = 'http://example.com/q=a'; But the resource I'm trying to access…

2008javascriptformspostsubmit·3 重點答案·1.9M 次瀏覽
1.8k

How to add a class to an HTML element with JavaScript?

I have an element that already has a class: <div class="someclass"> ... </div> How can i add a class to the div (not replace, but add) with JavaScript?

2009javascripthtmldom-manipulation·3 重點答案·2.8M 次瀏覽
1.7k

Differences between Lodash and Underscore.js

Why would someone prefer either the Lodash or Underscore.js utility library over the other? Lodash seems to be a drop-in replacement for underscore, the latter having been around…

2012underscore.jsjavascriptlodash·3 重點答案·374k 次瀏覽
1.7k

How do I find out which DOM element has the focus?

I would like to find out, in JavaScript, which element currently has focus. I've been looking through the DOM and haven't found what I need, yet. Is there a way to do this, and ho…

2009javascriptdom·3 重點答案·993k 次瀏覽
1.7k

Event binding on dynamically created elements?

I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off . This…

2008javascriptjqueryeventsunobtrusive-javascript·3 重點答案·1.1M 次瀏覽
1.7k

How to find the sum of an array of numbers

Given an array [1, 2, 3, 4] , how can I find the sum of its elements? (In this case, the sum would be 10 .) I thought $.each might be useful, but I'm not sure how to implement it.

2009javascriptjqueryarrays·3 重點答案·3.2M 次瀏覽
1.6k

How can I select an element by name with jQuery?

I have a table column I’m trying to expand and hide. jQuery seems to hide the <td> elements when I select it by class but not by the element’s name . For example: $(".bold").hide(…

2009javascriptjqueryhtmldomjquery-selectors·3 重點答案·2.4M 次瀏覽
1.5k

How to manage a redirect request after a jQuery Ajax call

I'm using $.post() to call a servlet using Ajax and then using the resulting HTML fragment to replace a div element in the user's current page. However, if the session times out,…

2008javascriptjqueryajaxhttp-redirect·3 重點答案·1.2M 次瀏覽
1.5k

Why don't self-closing script elements work?

What is the reason browsers do not correctly recognize: <script src="foobar.js" /> <!-- self-closing script element --> Only this is recognized: <script src="foobar.js"></script>…

2008javascripthtmlinternet-explorerxhtml·3 重點答案·177k 次瀏覽
1.5k

Check if checkbox is checked with jQuery

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array? I am using the following code, but it always returns the count of checked checkbox…

2010javascriptjqueryhtmlcheckbox·3 重點答案·2.8M 次瀏覽
1.5k

What are these three dots in React doing?

What does the ... do in this React (using JSX) code and what is it called? <Modal {...this.props} title='Modal heading' animation={false}>

2015javascriptreactjsspread-syntax·3 重點答案·668k 次瀏覽
1.5k

Check if element exists in jQuery

How do I check if an element exists if the element is created by .append() method? $('elemId').length doesn't work for me.

2011javascriptjquery·3 重點答案·1.8M 次瀏覽