像每个 jQuery迭代器函数都有类似这样的语法:
.each(function(index, element))
Run Code Online (Sandbox Code Playgroud)
这似乎意味着匹配此声明的函数必须采用2个参数.就像是:
function my_func(index, element){
alert(index+":"+element);
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这给出了两个可能的声明:
$("li").each(my_func);
Run Code Online (Sandbox Code Playgroud)
要么
$("li").each(function(index, element) {alert(index+":"+element);});
Run Code Online (Sandbox Code Playgroud)
第一个让我困惑,因为我没有看到index或element传递给我my_func.在jQuery中是否有一些魔法知道它提供了my_func2个参数each?
其次,如果我宣布了
var my_func= function(index, element){
alert(index+":"+element);
}
Run Code Online (Sandbox Code Playgroud)
这会改变什么.我的理解是,第一个是声明式,第二个是表达式,但是,在这种情况下,它们的行为应该相同吗?
最后,each我看到的大多数实现都是这样的:
$("li").each(function(){alert(this)});
Run Code Online (Sandbox Code Playgroud)
each回调的所有参数都是可选的吗?
我在使用$_SESSIONPHP变量更新数组元素时遇到问题.这是基本结构:
$product = array();
$product['id'] = $id;
$product['type'] = $type;
$product['quantity'] = $quantity;
Run Code Online (Sandbox Code Playgroud)
然后通过使用array_push()函数我在SESSION变量中插入该产品.
array_push($_SESSION['cart'], $product);
Run Code Online (Sandbox Code Playgroud)
现在这是我面临问题的主要部分:
foreach($_SESSION['cart'] as $product){
if($id == $product['id']){
$quantity = $product['quantity'];
$quantity += 1;
$product['quantity'] = $quantity;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在$_SESSION['cart']变量中增加产品数量.我怎样才能做到这一点?
我试图将PHP变量添加到href url
这是代码:
PHP
$uid= $_SESSION['userid'];
Run Code Online (Sandbox Code Playgroud)
HTML
<a href=http://example.com/uid= <?php echo ".$uid."?> /a>
Run Code Online (Sandbox Code Playgroud)
你如何添加,当我这样做时,这也是它的重定向:http://example.com/uid=.
我有一个名为“.error”的 div,如果提交注册表并且它有错误,它就会出现,否则不会出现。我正在尝试制作一个包含使用 if 结构的 jquery 的 javascript 语句。
For example
if error div is visible after submit button is clicked
remove the error div after several seconds
end if statement
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激
谢谢!
编辑:这是我的实际代码,我已尝试实现提供的一些示例,但到目前为止没有任何示例。
<script>
$(document).ready(function(){
var hideError = function () {
$(".error").hide();
};
$("registration-form").submit(function () {
setTimeout(hideError, 5000);
});
});
</script>
<?php
$validation_errors = '';
$proc_errors = '';
if (isset($_POST['register'])) { // User submitted registration form
require_once "formvalidator.php";
$validator = new FormValidator();
$validator->addValidation("new-username","req","Please enter a username");
$validator->addValidation("new-username","minlen=5","Username cannot be …Run Code Online (Sandbox Code Playgroud) 这是一个非常简单的问题,为什么在javascript中按标记获取元素不能像id一样获取元素?
这是我的代码示例:
http://codepen.io/vincentccw/pen/KvAfF
HTML
<ul>
<li>list</li>
<li>list</li>
<li>list</li>
<li>list</li>
<li>list</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
document.getElementsByTagName("li").style.color="red";
Run Code Online (Sandbox Code Playgroud) 我试图做到这样,当我点击 html 中的提交按钮时,我们在输入字段中的值替换 P 元素内具有 id“numberz”的项目。
我可以得到代码来做到这一点,但网站立即变回在 P 元素中使用默认值,其 id 为“numberz”。
如何防止网站改回硬编码到 HTML 文件中的元素的默认值?
<!DOCTYPE html>
<html>
<head>
<title>JS test</title>
<script>
function e() {
var x = document.getElementById("numberz");
var z = document.getElementById("num").value;
//alert(x.innerHTML + " s " + z);//
x.innerHTML= z;
};
</script>
</head>
<body>
<p>The number is:</p>
<p id = "numberz">s</p>
<br>
<br>
<form onsubmit = "return e();">
<input id = "num" type = "text" size = "4" placeholder = "test"/>
<input type = "submit" value = "Submit"/>
</form> …Run Code Online (Sandbox Code Playgroud) 为什么当我用javascript jquery添加相同的html代码时,我的保证金是否宽松?
尝试这个jsfiddle,你会理解我.
jQuery的
function addphone() {
$( "#phonedetails" ).append( '<div >' +
'<label>phone :</label>' +
'<input type="text" /> ' +
'<input type="button" onclick="addphone()"/>' +
'</div>');
}
Run Code Online (Sandbox Code Playgroud)
HTML
<div id="phonedetails">
<div>
<label>phone :</label>
<input type="text" />
<input type="button" onclick="addphone();"/>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 任何人都可以提供有关如何改进下面脚本的反馈意见吗?该脚本可以正常工作,但使用全局变量,我被告知使用全局变量可能会导致代码问题.
var vehicle = document.getElementById('vehicle');
var residence = document.getElementById('residence');
vehicle.setAttribute("class", "hide");
document.getElementById('myList').addEventListener('change', function(){
Array.prototype.forEach.call(document.querySelectorAll('.forms'), function(e){
e.setAttribute("class", "hide");
});
var sel=+this.selectedIndex - 2;
if(sel >= 0){
vehicle.setAttribute("class", "show");
residence.setAttribute("class", "hide");
} else {
residence.setAttribute("class", "show");
vehicle.setAttribute("class", "hide");
}
});
Run Code Online (Sandbox Code Playgroud) 我在Javascript中有一个数组,它通过Ajax传递给PHP脚本.
在file.js中:
var params = {};
params["apples"] = "five";
params["oranges"] = "six";
params["pears"] = "nine";
var ajaxData = {data : params};
fetchData(ajaxData);
function fetchData(arg) {
$.ajaxSetup ({
cache: false
});
request = $.ajax ({
type: "GET",
url: "script.php",
data: arg,
});
request.done(function(response){
$("#somediv").text(response);
});
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
}
Run Code Online (Sandbox Code Playgroud)
在script.php中:
<?php
$data = json_decode(stripslashes($_GET['data']));
var_dump($data);
?>
Run Code Online (Sandbox Code Playgroud)
var_dump的结果是:
object(stdClass)#1 (3) …Run Code Online (Sandbox Code Playgroud) javascript ×7
php ×4
html ×3
jquery ×3
css ×2
ajax ×1
arrays ×1
declarative ×1
expression ×1
function ×1
href ×1
json ×1
performance ×1
session ×1