gpt4 book ai didi

与动态添加的 HTML 元素一起使用时,Javascript 不会触发

转载 作者:行者123 更新时间:2023-12-01 03:22:07 24 4
gpt4 key购买 nike

我正在使用 this Stack Overflow answer 中的代码,该代码在单击时复制 altimg 属性,并在单击 textarea 时将该属性粘贴到 textarea 中。

该代码在页面加载时加载的静态 textarea 上运行良好,但我希望能够动态添加 textarea 。我在 HTML 顶部有一个按钮,它调用一个加载新 textarea 的函数。该函数位于 <script> 的最底部。

我无法将 alt 属性单击粘贴到动态添加的 textarea 中。我猜这与 document.getElementById(areaId); 这样的代码有关,它可能只在页面加载时被调用?如何使此行为绑定(bind)到动态创建的元素?我应该注意什么?

<html>
<body>

<!--dynamically add a text area-->
<button onclick="addImage()" style="cursor: pointer; position: absolute; top: 35px; left: 450px;">Add Image</button>

<img src="smiley1.png" alt="{smiley001}" style="cursor: pointer;">
<img src="smiley2.png" alt="{smiley002}" style="cursor: pointer;">
<img src="smiley3.png" alt="{smiley003}" style="cursor: pointer;">
<img src="smiley4.png" alt="{smiley004}" style="cursor: pointer;">
<img src="smiley5.png" alt="{smiley005}" style="cursor: pointer;">

<br>

<!--static textarea - this one works-->
<textarea id="imageText" cols="50" rows="10"></textarea>

</body>
</html>

<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>

<script>

selected = '';

$('img').click(function(){
console.log($(this).attr('alt'));
selected = $(this).attr('alt');
});

$('#imageText').click(function(){
insertAtCaret('imageText',selected)
// Clear the selection so it isn't copied repeatedly
selected = '';
});

function insertAtCaret(areaId,text) {
var txtarea = document.getElementById(areaId);
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false ) );
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
strPos = range.text.length;
}
else if (br == "ff") strPos = txtarea.selectionStart;

var front = (txtarea.value).substring(0,strPos);
var back = (txtarea.value).substring(strPos,txtarea.value.length);
txtarea.value=front+text+back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
range.moveStart ('character', strPos);
range.moveEnd ('character', 0);
range.select();
}
else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}

//add dynamic textarea - this does not take the alt attribute
function addImage() {
$("body").append('<div style="margin-bottom: 5px;"><textarea id="imageText" name="items[][image]" cols="25" rows="1" placeholder="Enter Image Here..."></textarea><button class="remove" style="margin-left: 5px;">Remove</button></div>');
}

</script>

最佳答案

您需要使用event delegation concept如下所示:-

$(document).on('click','img',function(){
console.log($(this).attr('alt'));
selected = $(this).attr('alt');
});

$(document).on('click','#imageText',function(){
insertAtCaret('imageText',selected)
// Clear the selection so it isn't copied repeatedly
selected = '';
});

注意:-不要使用live(),因为它已被弃用。

关于与动态添加的 HTML 元素一起使用时,Javascript 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45120215/

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