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

I have a layout similar to this: <div id="..."><img src="..."></div> and would like to use a jQuery selector to select the child img inside the div on click. To get the div , I've got this selector: $(this) How can I get the child img using a selector? The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selec

如何获得$(this)选择器的孩子?

我有一个类似于这样的布局: <div id="..."><img src="..."></div> 并希望使用jQuery选择器在点击时在div内选择子img 。 为了得到div ,我有这个选择器: $(this) 我如何使用选择器来获取孩子img ? jQuery构造函数接受一个名为context的第二个参数,它可以用来覆盖选择的上下文。 jQuery("img", this); 这和使用.find()一样的: jQuery(this).find("img"); 如果您希望的图片只是点击元素的直接后

Checking if a key exists in a JavaScript object?

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error? Checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually undefined ? var obj = { key: undefined }; obj["key"] != undefined // false, but the key exi

检查JavaScript对象中是否存在关键字?

如何检查JavaScript对象或数组中是否存在特定的键? 如果一个关键字不存在,并且我尝试访问它,它会返回false吗? 或者抛出一个错误? 检查undefined-ness不是测试密钥是否存在的准确方法。 如果密钥存在但该值实​​际上undefined怎样? var obj = { key: undefined }; obj["key"] != undefined // false, but the key exists! 你应该改用in运算符: "key" in obj // true, regardless of the actual value 如果你想检

Encode URL in JavaScript?

How do you safely encode a URL using JavaScript such that it can be put into a GET string? var myUrl = "http://example.com/index.html?param=1&anotherParam=2"; var myOtherUrl = "http://example.com/index.html?url=" + myUrl; I assume that you need to encode the myUrl variable on that second line? Check out the built-in function encodeURIComponent(str) and encodeURI(str) . In your case, thi

在JavaScript中编码URL?

你如何安全地使用JavaScript编码一个URL,以便它可以放入GET字符串中? var myUrl = "http://example.com/index.html?param=1&anotherParam=2"; var myOtherUrl = "http://example.com/index.html?url=" + myUrl; 我假设你需要在第二行上编码myUrl变量? 查看内置函数encodeURIComponent(str)和encodeURI(str) 。 在你的情况下,这应该工作: var myOtherUrl = "http://example.com/index.html?url=" + encode

Validate decimal numbers in JavaScript

What's the cleanest, most effective way to validate decimal numbers in JavaScript? Bonus points for: Clarity. Solution should be clean and simple. Cross-platform. Test cases: 01. IsNumeric('-1') => true 02. IsNumeric('-1.5') => true 03. IsNumeric('0') => true 04. IsNumeric('0.42') => true 05. IsNumeric('.42') => true 06. IsNumeric('99,999') =>

验证JavaScript中的十进制数

什么是最简洁,最有效的验证JavaScript中十进制数的方法? 奖励积分为: 明晰。 解决方案应该干净简单。 跨平台。 测试用例: 01. IsNumeric('-1') => true 02. IsNumeric('-1.5') => true 03. IsNumeric('0') => true 04. IsNumeric('0.42') => true 05. IsNumeric('.42') => true 06. IsNumeric('99,999') => false 07. IsNumeric('0x89f') => false 08. IsNumeric('#a

Change an element's class with JavaScript

如何使用JavaScript更改一个HTML元素的类以响应onClick事件? Modern HTML5 Techniques for changing classes Modern browsers have added classList which provides methods to make it easier to manipulate classes without needing a library: document.getElementById("MyElement").classList.add('MyClass'); document.getElementById("MyElement").classList.remove('MyClass'); if ( document.getElementById("MyEle

使用JavaScript更改元素的类

如何使用JavaScript更改一个HTML元素的类以响应onClick事件? 用于改变类的现代HTML5技术 现代浏览器已经添加了classList ,它提供了一些方法,使得在不需要库的情况下操作类变得更容易: document.getElementById("MyElement").classList.add('MyClass'); document.getElementById("MyElement").classList.remove('MyClass'); if ( document.getElementById("MyElement").classList.contains('MyClass') ) document.getEle

How do I correctly clone a JavaScript object?

I have an object, x . I'd like to copy it as object y , such that changes to y do not modify x . I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted properties. This isn't a problem, since I'm copying one of my own, literal-constructed objects. How do I correctly clone a JavaScript object? Updated answer Just use Object.assi

我如何正确克隆一个JavaScript对象?

我有一个对象, x 。 我想将它复制为对象y ,以便对y更改不会修改x 。 我意识到复制从内置JavaScript对象派生的对象将导致额外的不需要的属性。 这不是问题,因为我正在复制我自己的一个字面构造对象。 我如何正确克隆一个JavaScript对象? 更新了答案 只需使用Object.assign(),就像这里所建议的那样 过时的答案 为JavaScript中的任何对象执行此操作不会很简单或直接。 您将遇到错误地从对象的原型中拾取属性的

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 one is selected? To get the value of the selected radioName item of a form with id myForm : $('input[name=radioName]:checked', '#myForm').val() Here's an example: $('#myForm input').on('change', function()

我如何知道通过jQuery选择了哪个单选按钮?

我有两个单选按钮,并希望发布所选的值。 我如何通过jQuery获得价值? 我可以像这样得到他们所有的人: $("form :radio") 我如何知道选择哪一个? 要获取id为myForm的表单的所选 radioName项的值,请执行radioName : $('input[name=radioName]:checked', '#myForm').val() 这是一个例子: $('#myForm input').on('change', function() { alert($('input[name=radioName]:checked', '#myForm').val()); });<sc

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can you do the same in JavaScript? Use a sequential for loop: var myStringArray = ["Hello","World"]; var arrayLength = myStringArray.length; for (var i = 0; i < arrayLength; i++) { alert(myStringArray[i]); //

在JavaScript中循环访问数组

在Java中,您可以使用for循环遍历数组中的对象,如下所示: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } 你可以在JavaScript中做同样的事情吗? 使用顺序for循环: var myStringArray = ["Hello","World"]; var arrayLength = myStringArray.length; for (var i = 0; i < arrayLength; i++) { alert(myStringArray[i]); //Do something } @zipcodem

How do I empty an array in JavaScript?

Is there a way to empty an array and if so possibly with .remove() ? For instance, A = [1,2,3,4]; How can I empty that? Ways to clear an existing array A : Method 1 (this was my original answer to the question) A = []; This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually cr

如何在JavaScript中清空数组?

有没有办法清空数组,如果可能的话使用.remove() ? 例如, A = [1,2,3,4]; 我怎样才能清空它? 清除现有阵列的方法A : 方法1 (这是我对这个问题的原始答案) A = []; 这段代码将变量A设置为一个新的空数组。 如果你没有在其他地方引用原始数组A这是完美的,因为这实际上创建了一个全新的(空)数组。 你应该小心这个方法,因为如果你从另一个变量或属性引用这个数组,原始数组将保持不变。 只有在仅通过其原始

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): Methods for styling half of a character/letter Styling part of a character with CSS or JavaScript Apply CSS to 50% of a character Below is an example of what I am trying to obtain. Does a CSS or JavaScript solu

是否可以将CSS应用于角色的一半?

我在找什么: 设计一个角色的一个HALF的方式。 (在这种情况下,一半的字母是透明的) 我目前搜索并尝试过的(没有运气): 设计一半字符/字母的方法 使用CSS或JavaScript设计角色的一部分 将CSS应用于角色的50% 以下是我试图获得的一个例子。 是否存在一个CSS或JavaScript解决方案,或者我将不得不求助于图像? 我宁愿不去图像路由,因为这个文本最终会动态生成。 更新: 由于许多人问我为什么会想要塑造