所以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
,为什么?这对我未来的编码有何影响?
现在,这不只是一个有什么区别的问题,我已经做了一些测试(http://jsfiddle.net/ZC3Lf/)修改prop
和attr
的<form action="/test/"></form>?
与输出是:
1)prop修改测试
Prop:http://fiddle.jshell.net/test/1
Attr:http://fiddle.jshell.net/test/1
2)Attr修改测试
Prop:http://fiddle.jshell.net/test/1
Attr:/test/1
3)Attr然后Prop Propification测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/11
4)Prop然后Attr修改测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/11
现在我对一些事情感到困惑,据我所知:
Prop:在通过JavaScript
Attr进行任何修改后的当前状态值:在页面加载的html中定义的值.
现在如果这是正确的,
prop
似乎使action
完全合格,相反为什么修改属性不?prop
in 1)
修改属性,对我来说没有意义?attr
in 2)
修改属性,它们是否意味着以这种方式链接?HTML
JavaScript的
var element = $('form');
var property = 'action';
/*You should not need to modify below this line */
var body = …
Run Code Online (Sandbox Code Playgroud) 我想获得一个脚本,可以抓取用户的用户代理并将其支持到属性.
我正在制作网站问题联系表单,我通常需要知道用户使用的是什么浏览器.如何检测用户代理字符串并将其作为输入元素的值.
我的HTML看起来像:
<input type="hidden" id="UserAgent" name="User Agent" />
Run Code Online (Sandbox Code Playgroud)
我希望将用户代理作为value属性添加到它,因此它看起来像:
<input type="hidden" id="UserAgent" name="User Agent" value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" />
Run Code Online (Sandbox Code Playgroud) 我在一些地方看到.attr()用于jQuery.在一些地方.prop()被使用.但我搜索了SO和谷歌我很困惑.请告诉我这两者之间的确切区别以及何时使用它们.
我已经看到以下链接 jQuery attr与prop,有一个道具列表? jQuery attr vs prop?
但我没有得到答案.请帮帮我.谢谢.
在给出downvote之前请说明原因,然后我将在下一篇文章中更正.
我可以,一个jQuery1.9 +软件开发人员,attr()
在我的日常工作中"弃用"该方法的使用吗?
如许多问题所示,
关于"使用attr或使用prop?"有很多困惑.,并且,根据我的(开发人员)观点,对于attr()
方法的所有用途,我们可以使用prop
:
prop(name,newvalue)
方法更改所有值.removeProp(name)
方法都可以删除.attr(name)
方法也会受到影响.关于prop的"强类型":它比"html string value"(例如"checked"vs true)更好.attr
浏览器中的方法 返回undefined,如果不是)...嗯,我们需要在某个软件中使用它吗? 在表单中,".val()方法是建议的jQuery方式来获取或设置表单的值"所以,在这个时候(2013年),我没有看到attr
在开发新的jQuery代码时使用方法的一个很好的理由 ......但是,换句话说,这就是问题:我有充分的理由attr
在我的方法中使用 方法日常任务?
有.prop("disabled",false)的问题它在opera和firefox中工作得很好,但IE和chrome我无法让它工作..
Actualy它是一个邀请表单,我发送这样的发送按钮
<input id="sendInvite" class="mail_send" disabled="disabled" type="button" name="invite" value="Send">
Run Code Online (Sandbox Code Playgroud)
这是css
.mail_send[disabled="disabled"] {background-color:#343434; color:#747474}
Run Code Online (Sandbox Code Playgroud)
因此,当您看到按钮被禁用而您无法单击时,您必须首先写下您的名称和邮件,然后删除该按钮并发送邮件.对于这个我写的代码这里是:http: //pastebin.com/8u23G90b
但是这里出了点问题,在chrome和IE中禁用从未从按钮中删除,我也加载了jquery 1.7.1
抱歉我的英语
我有的html结构是这样的:
<ul id="something">
<li>
<a href="">
<img src="http://domain.com/directory/file1-128x79.jpg">
</a>
</li>
<li>
<a href="">
<img src="http://domain.com/directory/file2-128x79.jpg">
</a>
</li>
<li>
<a href="">
<img src="http://domain.com/directory/file3-128x79.jpg">
</a>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我正在尝试将文件名从文件#-128x79.jpg更改为文件#-896x277.jpg.
我不知道如何获取动态生成的文件名并搜索和替换src更改.
我找到了用'none'替换整个src的方法,以确保我到目前为止做到了,但我不知道如何做其余的事情.
$('#something').removeAttr('id').prop('class', 'some-class').find('img').prop('src', 'none');
Run Code Online (Sandbox Code Playgroud) 我正在使用 React 钩子作为应用程序状态,我想知道如何使用 props 初始化功能组件状态?该useState
钩医生说确切的东西一样,
const [count, setCount] = useState(0);
Run Code Online (Sandbox Code Playgroud)
我想通过传递给组件的 props 值来初始化该 0 值。年长的,
import React from 'react';
export default class Sym extends React.Component{
constructor(props){
super(props);
this.state = {
sym : [0,3,2,8,5,4,1,6],
active: this.props.activeSym
}
this.setActive = this.setActive.bind(this);
}
setActive(itemIndex){
this.setState({
active: itemIndex
});
}
render(){
return (
<div><h1>{ this.state.sym[this.state.active]}</h1></div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
工作正常。父组件传递activeSym
prop 并且Sym
组件使用this.props.activeSym
in初始化状态 constructor
。是否有任何解决方法可以在功能组件中实现相同的功能?
问题
\n在 React 中,当函数需要访问上述子组件的父组件中的状态时,我们可以将函数作为 props 传递到子组件中。我正在为需要实现此类行为的应用程序编写代码。但是,我无法找到在 Qwik 中定义函数的正确约定,然后发送它们。
\n试图
\n我尝试在界面中定义该函数,看看这是否有助于 Qwik 允许此实现,但到目前为止这也不起作用。
\n代码
\n我正在尝试从应用程序标题中包含的图标启动模式。我试图通过使用标头组件中声明的商店来控制模式的显示。它是一个布尔值,决定是否显示模式。我定义了用于修改标头组件中状态的函数,并尝试将其传递到我的模态子组件中。
\n// components/header/header.tsx\nimport { component$, useClientEffect$, useStore } from "@builder.io/qwik";\nimport { strictEqual } from "assert";\nimport Modal from "../modal/modal";\n\nexport default component$(() => {\n const store = useStore({\n ...\n modal: false\n });\n\n function onClose() {\n store.modal = false;\n }\n\n return (\n <header>\n {store.modal && <Modal onClose={onClose}/>}\n <div \n onClick$={()=>{\n store.modal = true;\n }}\n >\n <i class="fa-solid fa-cart-shopping"></i>\n </div>\n </header>\n );\n});\n
Run Code Online (Sandbox Code Playgroud)\n在我的模态组件中,我尝试使用一个界面来指示我正在将一个函数传递到我的 props …
prop ×10
jquery ×8
attr ×5
javascript ×5
attributes ×1
deprecated ×1
dom ×1
node.js ×1
qwik ×1
react-hooks ×1
reactjs ×1
src ×1
state ×1
user-agent ×1