我有一个输入类,我想将其更改为选择表单属性。
到目前为止,我的 h2.title 上有类似的东西,将单词从 Apple 更改为 Orange。
示例
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
if (){
$(document).ready(function(){
$(document).live({
mouseover: function(e) {
$('h2.title').each(function() {
var text = $(this).text();
text = text.replace('Apple', 'Orange');
$(this).text(text);
});
}
});
});
}
</script>
我真的很喜欢将其转换为选择表单属性
<input class = "form-input ng-pristine ng-untouched ng-valid ng-empty ng valid-maxlength" id="Banana" maxlength = "2000" name="Banana" type=text"
有办法解决这个问题吗?有 2 个选择选项
最佳答案
是的,你可以这样做:
text = text.replace('Apple', $(this).attr("name"));
这是一个演示:
$(document).ready(function() {
$(document).on("mouseover", function(e) {
$('h2.title').each(function() {
var text = $(this).text();
text = text.replace('Apple', $(this).attr("name"));
$(this).text(text);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="title" name="Banana">Apple</h2>
<h2 class="title" name="Orange">Apple</h2>
<h2 class="title" name="Pineapple">Apple</h2>
<h2 class="title" name="Grape">Apple</h2>
关于javascript - jQuery 根据属性更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108014/