2 回答
TA贡献1946条经验 获得超4个赞
您可以在每个锚标记上使用事件侦听器,并通过ID设置输入的值,您需要相应地更改它:
const elements = document.getElementsByClassName('getTitle')
const input = document.getElementById('[change_this]')
Array.from(elements).forEach(a => {
a.addEventListener('click', (el) => {
input.value = el.target.title
})
})
<div class="all">
<div class="myOtherClass">
<a class="getTitle test" Title="i want grab this title">click me to change</a>
</div>
<div class="myOtherClass">
<a class="getTitle test" Title="i want grab this too">click me to change</a>
</div>
<div class="myOtherClass">
<a class="getTitle test" Title="same at 2 other">click me to change</a>
</div>
<div class="Otherclass">
<input id="[change_this]" class="myInput" type="button" value="" />
</div>
TA贡献1752条经验 获得超4个赞
从什么可以理解到你做什么:
document.querySelectorAll('.getTitle').forEach( Z_elm => {
Z_elm.addEventListener('click', function () {
document.querySelector('.myInput').value = this.title;
// or : (in your real code)
// document.querySelector('#myModal .getinput').value = this.title;
})
})
a {cursor: pointer;}
<div class="all">
<div class="myOtherClass">
<a class="getTitle" Title="i want grab this title">click me to change</a>
</div>
<div class="myOtherClass">
<a class="getTitle" Title="i want grab this too">click me to change</a>
</div>
<div class="myOtherClass">
<a class="getTitle" Title="same at 2 other">click me to change</a>
</div>
<div class="Otherclass">
<input class="myInput" type="button" value=""/>
</div>
</div>
添加回答
举报