javascript - 使用 jQuery 和 Javascript 获取动态生成的 id
问题描述
新来的,需要一些帮助。我正在开发一个项目,并在其中创建了一些具有动态生成 id 的元素,但我无法定制现有代码以使用动态 id。这是我的 HTML:
这是我的工作 JS 脚本:
$('#headline1').keyup(updateCount).keydown(updateCount);
document.getElementById("headline1").addEventListener("change", updateCount);
function updateCount() {
var cs = $(this).val().length;
$('#countHeadline1').text(cs);
cs = $(this).val();
$('#headlineText1').text(cs);
}
这是我在其他解决方案中尝试过的,但似乎没有任何效果:
var c = 1,
var headline1 = "headline1" + c;
var countHeadline1 = "countHeadline1" + c;
var headlineText1 = "headlineText1" + c;
$('#' + headline1).keyup(updateCount).keydown(updateCount);
document.getElementById("headline1" + c).addEventListener("change", updateCount);
function updateCount() {
var cs = $(this).val().length;
$('#' + countHeadline1).text(cs);
cs = $(this).val();
$('#' + headlineText1).text(cs);
}
c += 1;
谢谢,
编辑:这是在单击按钮时创建元素的代码:
jQuery('#add').on('click', function() {
c += 1;
jQuery('#parent').append('<hr><div>Headline 1:<br /><div><INPUT TYPE="text" placeholder="Headline" onkeydown="javascript:stripspaces(this)" id="headline1'+ c +'" maxlength="30" style="width: 350px;float:left;min-height: 32px;"><span id="countHeadline1'+ c +'" class="adCounter" style="color: rgb(255, 255, 255);float:left;position: inherit;margin-left: 0px;">0</span></div></div>');
});
解决方案
另一种方法是对动态创建的元素使用事件委托。
另一种方法是存储有用信息data-attributes
的功能$data
以及存储有用信息的功能,在这种情况下,id
是创建元素的当前。
此外,此方法使用 CSS 类inputs
来识别文本字段并绑定事件input
以针对这些文本字段上的任何更改执行函数updateCount
。
function updateCount() {
var identifier = $(this).data('identifier'); //'data attribute' along with function 'data'.
$('#countHeadline1' + identifier).text($(this).val().length);
$('#headlineText1' + identifier).text($(this).val());
}
var c = 1;
$('#add').on('click', function() {
c += 1;
$('#parent').append('<br><br><br>Headline ' + c + ':<br /><INPUT TYPE="text" class="inputs" data-identifier="' + c + '" placeholder="Headline" id="headline1' + c + '" maxlength="30" style="width: 350px;float:left;min-height: 32px;"/><span id="countHeadline1' + c + '" class="adCounter" style="color: rgb(0, 0, 0);float:left;position: inherit;margin-left: 0px;">0</span><br><span id="headlineText1' + c + '" class="headLine" style="color: rgb(0, 0, 0);float:left;position: inherit;margin-left: 0px;">0</span>');
});
$('#parent').on('input', '.inputs', updateCount); // Event delegation
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='parent'>
Headline 1:
<br>
<INPUT class='inputs' TYPE="text" data-identifier='1' placeholder="Headline" id="headline11" maxlength="30" style="width: 350px;float:left;min-height: 32px;" />
<span id="countHeadline11" class="adCounter" style="color: rgb(0, 0, 0);float:left;position: inherit;margin-left: 0px;">
0
</span><br>
<span id="headlineText11" class="headLine" style="color: rgb(0, 0, 0);float:left;position: inherit;margin-left: 0px;">
0
</span>
</div><br>
<button id='add'>Add</button>
重要提示:我已删除 this onkeydown="javascript:stripspaces(this)"
,因此您需要决定在何处调用该函数。
推荐阅读
- firebase - 在 localhost 上停止 firebase 模拟后防止擦除数据
- android - 如何在 Android Studio 中导入 OpenCV 4.5
- swift - SwiftUI 中 macOS 的侧边栏菜单
- javascript - Node JS 中的字符串相等
- java - 活动在前台时如何在 Android 上播放音乐
- jestjs - 什么是 nestjs typeorm 中的 getRepositoryToken 以及何时使用它?
- python - 检查元组列表中的字符串是否包含空格,如果为真则删除元组
- python - UnicodeDecodeError:将以太坊私钥的python字节串转换为字符串?
- arrays - 如何正确地将获取的对象数组设置为响应状态并在 FlatList 中呈现数据
- javascript - String.equals 不是 Web Worker 中的函数