从弹出窗口使用jquery替换文本区域中的img src
Replacing img src in textarea using jquery from a pop-up window
我正在尝试从弹出窗口使用jquery替换父窗口中的文本区域和输入框图像源。输入框中的文本更改没有任何问题,但是文本区域框中的文字保持不变。
这是父窗口的代码:
<textarea cols="100" rows="20" class="editor">
<a href="http://www.amazon.com">
<img src="image.jpg" alt="replace image source in this textbox" />
</a>
</textarea>
<input type="text" value="image.jpg" maxlength="255" MultiLine="false" Class="inputBox" style="width:875px;" />
<a href="/PopUpBox" class="popup">Click Here To Add An Image/s</a>
<script type = "text/javascript">
$('.popup').click(function (event) {
event.preventDefault();
window.open($(this).attr("href"), "popupWindow", "width=750, height=800, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes");
});
</script>
这是弹出窗口的代码:
<div class="selectButton">
<input id="select" class="selectImage" type="button" data-imagepath="image2.jpg" value="Select">
</div>
<script type = "text/javascript">
$('.selectImage').live("click", function (e) {
var selectImage = $(this).data("imagepath");
window.opener.$(".editor img").attr("src", selectImage); // can't change img src in textarea box
window.opener.$(".inputBox").val(selectImage);
self.close();
e.preventDefault();
});
</script>
有什么想法为什么不起作用吗?
您无法在文本区域中获取图像源。Textarea以文本而非元素的形式响应代码。
您可以使用div并使其内容可编辑。
<div contenteditable="true"></div>
或者获取editor的值并将其粘贴到div中,从div 中选择您的图像
var editor = $(".editor").val();
$(".output").html(editor);
jQuery live函数不赞成使用"0n"
尝试用更新的HTML字符串替换文本区域值,如下所示:
<script type = "text/javascript">
$('.selectImage').live("click", function (e) {
var selectImage = $(this).data("imagepath");
window.opener.$(".editor img").val(
'<a href="http://www.amazon.com">'
+ '<img src="'+selectImage+'" alt="replace image source in this textbox" />'
+'</a>');
window.opener.$(".inputBox").val(selectImage);
self.close();
e.preventDefault();
});
</script>
在弹出窗口中,只需替换此JS代码:
<script type = "text/javascript">
$('.selectImage').click(function (e) {
var selectImage = $(this).data("imagepath");
window.opener.$(".editor img").attr("src", selectImage); // can't change img src in textarea box
window.opener.$(".inputBox").val(selectImage);
self.close();
e.preventDefault();
});
</script>
@Vikram Deshmukh在右边@Olalekan也是正确的,文本区域的内容是文本,它不能像HTML那样被操纵。
我在这里发布了同样的问题来获得答案:
https://forum.jquery.com/topic/replacing-img-src-in-textarea-using-jquery-from-a-pop-up-window-25-9-2014
以下是有效的解决方案:
<script type = "text/javascript">
$('.selectImage').live("click", function (e) {
var selectImage = $(this).data("imagepath");
window.opener.$('.editor').val(function (i, value) {
return value.replace(/src="[^"]+"/, 'src="' + selectImage + '"');
});
window.opener.$(".inputBox").val(selectImage);
self.close();
e.preventDefault();
});
</script>
谢谢你的帮助。
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- HTML文本区域文本到XML
- 阻止在文本区域/文本框中输入数字
- 使用 javascript 单击图片后更改<文本区域>文本
- 使用 javascript 检测文本区域/文本输入中的 HTML 标记,但它应该支持常见的>和<
- 在按 Tab 键时选择文本区域文本
- 切换文本区域文本,但保留对文本的更改
- 清除文本区域/文本框内容的按钮
- 加载页面时,聚焦并全选文本区域文本
- 如何使可编辑文本区域文本不可选择
- 我的文本区域文本是't用javascript将我想要的样式设置为CSS
- 使用jQuery将文本区域文本安全地存储在隐藏字段中,然后稍后在PHP中使用该文本
- 删除文本区域文本提交在新的选项卡
- 如何在添加新文本时保持文本区域文本静态
- 使用 jQuery 将文本区域文本设置为格式正确的 HTML 代码
- 为什么文本添加到区域文本是通过页面刷新,而不是直接刷新
- 使用 jquery 修改文本区域文本
- 如何使用Javascript在文本区域(文本框)中单击(光标闪烁)而不使用鼠标单击