gpt4 book ai didi

javascript - 将按钮操作附加到其内部的 div 上?

转载 作者:行者123 更新时间:2023-12-02 20:11:39 26 4
gpt4 key购买 nike

Edit What I'm actually asking if I can target the div my button is inside without knowing the name of the id?

我想编写一个 JavaScript 函数来更改输入框和按钮本身的属性,但我想在不为每个按钮和每个输入框创建唯一 ID 的情况下执行此操作。

假设我有一个像这样的 onclick javascript

function changeid ()
{
var e = document.getElementById("changebutton");
e.id = "savebutton";
$('#TheAttachedDiv').attr('readonly', false);
}
function changeid ()
{
var e = document.getElementById("savebutton");
e.id = "changebutton";
$('#TheAttachedDiv').attr('readonly', true);
}

由于其他原因,我将拥有 4 个具有相同类的字段,因此我不希望在单击除 1 个 div 之外的按钮时更改所有 CHANGE 按钮和 DIVS 的属性。

有办法做到这一点吗?谢谢

我正在做什么:

<div class="question active">Q1
<input id="one" type="text">
</div>
<div class="button passive">
<input type="button" value="Change" id="changeone" onclick="changeid()">
</div>

同一个 div 不断重复。

最佳答案

尝试 jQuery 方法 .parent()如果您想要直接父级或 .parents( ... )如果你想搜索 DOM 树更高层的祖先。

关于javascript - 将按钮操作附加到其内部的 div 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com