编辑 - 2个优雅的解决方案
来自Pranav和Bekim的两个优雅解决方案.谢谢,测试和完美工作.
一
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
Run Code Online (Sandbox Code Playgroud)
二
var res = data.slice(),
len = res.length;
for (var i = 0; i < len; i++) {
if (res[i].name == 'other') {
res.push(res.splice(i, 1)[0]);
i--;
len--;
}
}
Run Code Online (Sandbox Code Playgroud)
JS TOOLS IM使用
Angular 1.5.6,lodash 4.1x
这是我有一个按字母顺序排序的对象数组的场景,例如下面的sortedData等.但是,在该数组中也是所有Other明显按字母顺序排序的catch .我想从数组中删除其他,然后移动到数组的末尾而不会弄乱当前的排序数组.
注意
我现在的方法可行,但很难看.JS,棱角分明还是lodash有更优雅的东西吗?
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
function move(array, fromIndex, toIndex) {
array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
return array;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试更改一个select optiononclick但我不想使用option value. 如果我给我的按钮一个,我的代码就可以工作,value='3'但我想要的是选择data-price="0"在我的情况下是的那个value='3'.
JS:
jQuery(document).ready(function () {
jQuery('#sample-addtocart-button').click(function(){
jQuery('.product-custom-option').val(jQuery(this).attr('value'));
});
});
Run Code Online (Sandbox Code Playgroud)
网址:
<button value="0" id="sample-addtocart-button" type="button">Free</button>
<select class="product-custom-option">
<option value="">-- Please Select --</option>
<option data-price="10" value="1">£10.00</option>
<option data-price="20" value="2">£20.00</option>
<option data-price="0" value="3">FREE</option>
</select>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
我试图为此目的测试password_hash方法我创建了以下函数hashPassword:
function hashPassword($string) {
$settings = array('cost' => 10, 'encryption_key' => 'thisIsMyEncryptionKey1234');
return password_hash($string, PASSWORD_BCRYPT, $settings);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我用随机字符串测试这个 "test"
结果将是:
$2y$10$thisIsMyEncryptionKeyu5n3NNnKh3DjgJqgb5pE8YOLBclKrVWC
Run Code Online (Sandbox Code Playgroud)
或者如果我测试它helloworld:
$2y$10$thisIsMyEncryptionKeyuVw8QRVNw8HbEWHX2oQlArVtne2TzOpS
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么会这样?还是假设是这样的?
我让这个页面的静态版本在本地工作得很好,但我公司使用的CMS在CMS中实现时会发出可怕的代码.
最初我试图循环遍历div中的所有div并构建一个包含结果的选择框.
<div id="products-list">
<div id="glasgow">
<div class="product">
<!--Content-->
</div>
</div>
<div id="edinburgh">
<div class="product">
<!--Content-->
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用一个简单的每个循环与第一级div是好的.
$("#products-list > div").each(function() {
if ($(this).attr("id") != undefined || $(this).attr("id") != null) {
$('#select-example').append($('<option>', {
value: $(this).attr("id"),
text: $(this).attr("id")
}));
}
});
Run Code Online (Sandbox Code Playgroud)
但是我的CMS决定像这样包装div,而且我无能为力.我正在尝试返回ID"Glasgow"和"Edinbugh".
<div id="products-list">
<div class="w-component-wrapper">
<div>
<div class="w-component-content">
<div id="glasgow">
<div class="product">
<!-- Content -->
</div>
</div>
</div>
</div>
</div>
<div class="w-component-wrapper">
<div>
<div class="w-component-content">
<div id="edinburgh">
<div class="product">
<!-- Content -->
</div>
</div>
</div>
</div>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) 我有以下代码,显示悬停图像时的一些文本.但是当光标在h1标签上移动时有一些问题,那时它正在闪烁.为什么会这样?
jQuery(function($) {
$('.content1').mouseover(function() {
$('.content').show();
});
$('.content').mouseout(function() {
$('.content').hide();
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img class="content1" src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/2000px-HTML5_logo_and_wordmark.svg.png" style="width:100%;position:relative">
<div class="content" style="position: absolute; width: 100%; height: 100%; left: 0px; top: 0px; text-align: center; color: white; display: none; background: rgba(0, 0, 0, 0.901961);">
<h1 style="color:white">hiiiiiiiiiiiiiiiiiii</h1>
</div>Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用 jQuery 代码在选择所有 html 选择下拉值时启用按钮,或者在未选择单个值时禁用按钮。
这是到目前为止我已经尝试过的,但我的代码无法正常工作。有什么建议吗?
$(function() {
$('.picker').on('change', function() {
var SelectList = $('.picker');
//Here i'll find how many dropdown are present
for (var i = 0; i < SelectList.length; i++) {
//Here i need to check each dropdown value whether it selected or not
if (SelectList[i].val() != "") {
//If all dropdown is selected then Enable button
$("#Testing").attr("disabled", true);
} else {
//Disable button if any dropdown is not selected
$("#Testing").attr("disabled", false);
}
}
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> …Run Code Online (Sandbox Code Playgroud)我有一个可编辑的链接,如果有人将鼠标悬停在该链接上,则悬停时应该有一个 fa fa-edit 图标
这是html代码
<a href="#" class="btn" id="company_name" data-type="text" data-pk="3" data-name="company_name" data-title="Enter Company Name">Learn web programming</a> <i class="fa fa-pencil"></i>
Run Code Online (Sandbox Code Playgroud)
我只想在悬停时显示 fa fa-pencil 图标
嗨,我正在尝试使用for循环在javascript中推送数组内的javascript对象来迭代数据.这是我的代码的样子.
var data = {"up": [{
"name": "jack",
"age" : 10
},
{
"name" : "jhon",
"age" : 12
}]};
var output = {};
var output_data = {
element: []
};
for (var key in data.up) {
output.user_name = data.up[key].name;
output_data.element.push(output);
}
console.log(output_data.element);
Run Code Online (Sandbox Code Playgroud)
但是,正如您在示例http://jsbin.com/fanazaxoda/edit?html,js,console中看到的那样,只有第二个元素名称'jhon'被插入两个世界中.我在这做错了什么?请解释.
我正在尝试使用输入来允许最终用户href使用JavaScript 更改元素,我似乎无法使事情正常运行.
这是我到目前为止所拥有的......
<input type="text" id="feed" />
<a id="display" href="">#</a>
Run Code Online (Sandbox Code Playgroud)
$('#feed').keyup(function () {
var temp = "";
temp = $('#feed').text.val();
$('#display').href = temp;
});
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢!
html ×6
javascript ×6
jquery ×6
css ×2
angularjs ×1
arrays ×1
each ×1
font-awesome ×1
html-table ×1
lodash ×1
loops ×1
php ×1