我正在尝试设置复选框列表的样式.我添加了我的样式,它们在渲染时显示正确.我想在单击复选框的标签时添加一个类.这是我的标记,这在jsfiddle中是相同的.您可以从我的小提琴中看到,只需单击一下即可注册两个点击事件.为什么?
HTML:
<ul>
<li>
<label for="test_0" class="">
<input id="test_0" name="offering_cycle" type="checkbox" value="1"> Fall
</label>
</li>
<li>
<label for="test_1" class="">
<input id="test_1" name="offering_cycle" type="checkbox" value="2"> Spring
</label>
</li>
<li>
<label for="test_2" class="">
<input id="test_2" name="offering_cycle" type="checkbox" value="3"> Summer
</label>
</li>
<li>
<label for="test_3" class="">
<input id="test_3" name="offering_cycle" type="checkbox" value="4"> Other
</label>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
CSS:
ul {
list-style-type:none;
}
label {
position:relative;
display:inline-block;
padding-left:27px;
height:25px;
}
label:before {
display:block;
position:absolute;
top:-2px;
margin-left:-28px;
width:18px;
height:18px;
background-color:#fff;
border-radius:5px;
border:1px solid #ccc;
text-align: …
Run Code Online (Sandbox Code Playgroud) 我有一个导航,初始网址如下所示:
test.php?query=19
我的页面上有链接 <a href="#section-1">Section 1</a><a href="#section-2">Section 2</a><a href="#section-3">Section 3</a>
有3个部分:
<section id="section-1"></section><section id="section-2"></section><section id="section-3"></section>
Run Code Online (Sandbox Code Playgroud)
我正在使用此jquery代码从页面顶部(或用户在页面上的位置)滚动到该部分到该部分的顶部,而不是#
在我的网址中显示标记.
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 2000);
return false;
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,这不会滚动到该部分.它只是转到该部分的底部,然后滚动到顶部,并#
显示在我的网址中.
我的主页上有另一个菜单:
home.php
我使用完全相同的jquery代码,它适用于该页面.
我的问题是如何让ScrollTop在我的test.php?query=19
页面中工作就像home.php
在我点击test.php?query=19
我的网址上的内容时更改为:test.php?query=19#section-1
有没有办法用jQuery找到所有数字类?
我有以下类的元素:
<div class="placeholder 0 original dropped default-load"></div>
<div class="placeholder 1 original dropped default-load"></div>
<div class="placeholder 2 original dropped default-load"></div>
<div class="placeholder 3 original dropped default-load"></div>
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用jQuery draggable ui.因此这些占位符是可拖动的,并且最终这些数字类将以随机顺序例如(3,0,2,1),并且将不再与index
if if use the .each
function 匹配.
所以基本上,在页面加载时,元素的顺序为0,1,2,3 ......(基于数据库中的结果数量).
他们可以乱七八糟地这将导致随机顺序(0,3,2,1,...).但是有一个默认按钮.使用此按钮,他们可以撤消所有操作,并重置默认顺序.
我尝试了以下但是这不起作用,因为index
它们与数字类不匹配,如果他们乱七八糟(他们显然会这样).
$(".default").click(function (e) {
e.preventDefault();
$("li.placeholder").each(function (index) {
$(this).empty();
$(this).removeClass(index);
$(this).removeClass("dropped");
$(this).removeClass("default-load");
if (!($(this).hasClass("original"))) {
$(this).remove();
}
$(".modal-" + index).remove();
});
init(); // Callback
});
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!!
任何人都可以告诉我这两个脚本之间有什么区别,我不是javascript/jquery专家.
$(document).on("click", "a", function () {
i = $(this).data("value");
alert(i)
})
$("a").click(function () {
i = $(this).data("value");
alert(i)
});
Run Code Online (Sandbox Code Playgroud) 请看下面的脚本.我正在使用Chrome进行测试.
/*declare a new set*/
var items = new Set()
/*add an array by declaring as array type*/
var arr = [1,2,3,4];
items.add(arr);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4]}
/*add an array directly as argument*/
items.add([5,6,7,8]);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8]}
/*print type of items stored in Set*/
for (let item of items) console.log(typeof item); //object, object
/*check if item has array we declared as array type*/
console.log(items.has(arr)); // …
Run Code Online (Sandbox Code Playgroud) 为什么以下行导致Node.js中的运行时错误?
var a = ````;
Run Code Online (Sandbox Code Playgroud)
抛出:
TypeError:""不是函数
使用Node.js版本4.x,6.x,8.x和9.x进行测试
如何检查文本框是否仅包含数字?
谷歌搜索时我遇到了这个.但我想知道是否isNumeric
可以用于此目的,或者是否有更简单的方法来检查文本框是否具有数值.
var query = $('#myText').val();
if (parseFloat(query) == NaN) {
alert("query is a string");
} else {
alert("query is numeric");
}
Run Code Online (Sandbox Code Playgroud) 我有一个阵列
var arr = [' A ', ' b ', 'c'];
Run Code Online (Sandbox Code Playgroud)
而且我想 trim
从数组中的每个元素的空格.
它可以通过使用Array.map
as 来完成
arr.map(function(el) {
return el.trim();
});
Run Code Online (Sandbox Code Playgroud)
我很好奇将trim
/ toLowerCase
函数直接传递给map
as回调函数,如arr.map(Math.max.apply.bind(Math.max,null)); 从每个子数组或arr.map(Number)获取最大元素; 将每个元素转换为Number.
我试过了
arr.map(String.prototype.trim.apply);
Run Code Online (Sandbox Code Playgroud)
但这是投掷错误
未捕获的TypeError:在undefined上调用了Function.prototype.apply,这是一个未定义的而不是函数
我希望String.prototype.trim.apply
应该为数组中的每个元素调用,并将上下文设置为array中的元素(传递给apply
);
map
map
我需要将返回值存储在变量中.
我想知道如何修复此代码.我知道如果b的值为3,我会得到值= 2,但如果函数执行多次迭代,我就会被识别出来.我读到我应该使用回调或其他东西,但我不知道如何,也解释为什么我的代码不起作用,我应该如何解决它.(当然这段代码是出于演示的目的,好像我会告诉你它原来可能会让人感到困惑.)非常感谢!
var b = 70;
function myfunction() {
b--;
if (b < 3) {
return b;
} else {
myfunction();
}
}
value = myfunction();
console.log(value);
Run Code Online (Sandbox Code Playgroud) 我需要解析一个对象并使用Underscore/Lo-Dash转换为一个数组
尝试使用usersocre,但它没有得到预期的结果.对不起我是下划线js的新手.非常感谢您的帮助.
var arr = _.values(obj)
var obj = {
'2c13790be20a06a35da629c71e548afexing': [{
connector: '',
displayName: 'John',
loginName: '',
userImage: '2.jpg'
}],
'493353a4c5f0aa71508d4055483ff979linkedinpage': [{
connector: '',
displayName: 'Mak',
loginName: '',
userImage: '1.jpg'
}]
}
Run Code Online (Sandbox Code Playgroud)
预期的输出数组
array = [{
connector: '2c13790be20a06a35da629c71e548afexing',
displayName: 'John',
loginName: '',
userImage: '2.jpg'
}, {
connector: '493353a4c5f0aa71508d4055483ff979linkedinpage',
displayName: 'Mak',
loginName: '',
userImage: '1.jpg'
}]
Run Code Online (Sandbox Code Playgroud) javascript ×9
jquery ×5
arrays ×1
css ×1
ecmascript-6 ×1
html ×1
jquery-ui ×1
lodash ×1
node.js ×1
prototype ×1
recursion ×1
regex ×1
return ×1
return-value ×1