所以jQuery 1.6具有新功能prop().
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
Run Code Online (Sandbox Code Playgroud)
或者在这种情况下,他们做同样的事情?
如果我也有转用prop(),所有的旧attr()电话,如果我切换到1.6将打破?
UPDATE
selector = '#id'
$(selector).click(function() {
//instead of:
var getAtt = this.getAttribute('style');
//do i use:
var thisProp = $(this).prop('style');
//or:
var thisAttr = $(this).attr('style');
console.log(getAtt, thisProp, thisAttr);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id='id' style="color: red;background: orange;">test</div>Run Code Online (Sandbox Code Playgroud)
(另见这个小提琴:http://jsfiddle.net/maniator/JpUF2/)
控制台会记录getAttribute作为一个字符串,并attr作为一个字符串,但prop作为一个CSSStyleDeclaration,为什么?这对我未来的编码有何影响?
我正在使用jQuery,并想知道我是否应该使用val()或text()(或其他方法)来读取和更新textarea的内容.
我已经尝试了两种方法,但我遇到了两个问题.当我使用text()更新textarea时,换行符(\n)不起作用.当我使用val()来检索textarea内容时,如果文本太长,则文本会被截断.
如何使用jQuery识别空文本框?如果可能的话,我想使用选择器来做.此外,我必须选择id,因为在我想要使用它的真实代码中我不想选择所有文本输入.
在我的以下两个代码示例中,第一个示例准确地显示用户在文本框"txt2"中键入的值.第二个示例确定存在空文本框,但如果填写它仍然将其视为空.为什么是这样?
这可以仅使用选择器来完成吗?
此代码报告文本框"txt2"中的值:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#cmdSubmit').click(function() {
alert($('[id=txt2]').val());
});
});
</script>
</head>
<body>
<form>
<input type="text" name="txt1" id="txt1" value="123" /><br />
<input type="text" name="txt2" id="txt2" value="" /><br />
<input type="text" name="txt3" id="txt3" value="abc" /><br />
<input type="submit" name="cmdSubmit" id='cmdSubmit' value="Send" /><br />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此代码始终将文本框"txt2"报告为空:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#cmdSubmit').click(function() {
if($('[id^=txt][value=""]').length > 0) {
if (!confirm("Are you sure you want to submit empty fields?")) { …Run Code Online (Sandbox Code Playgroud) 如何检查节中的(required)输入字段的空值,然后使用jQuery在事件上为它们添加一个类?到目前为止,我尝试过:
jQuery("#sender_container input.required").val("").addClass("error");
Run Code Online (Sandbox Code Playgroud)
但这似乎是设定价值,而不是检查它.有任何想法吗?