Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从弹出窗口使用jquery替换textarea中的img src_Javascript_Jquery_Html_Popup_Textarea - Fatal编程技术网

Javascript 从弹出窗口使用jquery替换textarea中的img src

Javascript 从弹出窗口使用jquery替换textarea中的img src,javascript,jquery,html,popup,textarea,Javascript,Jquery,Html,Popup,Textarea,我正在尝试使用弹出窗口中的jquery替换父窗口中的textarea和InputBox图像源。输入框中的文本更改没有任何问题,但textarea框中的文本保持不变 以下是父窗口的代码: <textarea cols="100" rows="20" class="editor"> <a href="http://www.amazon.com"> <img src="image.jpg" alt="replace image source in

我正在尝试使用弹出窗口中的jquery替换父窗口中的textarea和InputBox图像源。输入框中的文本更改没有任何问题,但textarea框中的文本保持不变

以下是父窗口的代码:

<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>

$('.popup')。单击(函数(事件){
event.preventDefault();
window.open($(this.attr(“href”),“popupWindow”,“宽度=750,高度=800,工具栏=no,位置=no,目录=no,状态=no,菜单栏=no,滚动条=yes”);
});
以下是弹出窗口的代码:

<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>   

$('.selectImage').live(“单击”),函数(e){
var selectImage=$(this.data(“imagepath”);
window.opener.$(“.editor img”).attr(“src”,selectImage);//无法在文本区域框中更改img src
window.opener.$(“.inputBox”).val(选择图像);
self.close();
e、 预防默认值();
});   

你知道为什么这不起作用吗?

你无法在文本区域中获取图像源。文本区域以文本而非元素的形式响应代码

可以使用div并使其可编辑

<div contenteditable="true"></div>

不推荐使用jQuery live函数使用“0n”

尝试用更新的HTML字符串替换textarea值,如下所示:

<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>  

$('.selectImage').live(“单击”),函数(e){
var selectImage=$(this.data(“imagepath”);
window.opener.$(“.editor img”).val(
'');
window.opener.$(“.inputBox”).val(选择图像);
self.close();
e、 预防默认值();
});   

在弹出窗口中,只需替换此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>   

$('.selectImage')。单击(函数(e){
var selectImage=$(this.data(“imagepath”);
window.opener.$(“.editor img”).attr(“src”,selectImage);//无法在文本区域框中更改img src
window.opener.$(“.inputBox”).val(选择图像);
self.close();
e、 预防默认值();
});   

@Vikram Deshmukh的观点是正确的@Olalekan也是正确的,textarea的内容是文本,它不能像HTML那样操作

我在这里发布了同样的问题以获得答案:

以下是有效的解决方案:

<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>  

$('.selectImage').live(“单击”),函数(e){
var selectImage=$(this.data(“imagepath”);
window.opener.$('.editor').val(函数(i,值){
返回值。replace(/src=“[^”]+“/,'src=“”+selectImage+”);
});
window.opener.$(“.inputBox”).val(选择图像);
self.close();
e、 预防默认值();
});   

感谢您的帮助。

我会将动态内容放入文本区域,因此此解决方案不适用。很抱歉,这涉及到弹出窗口。我认为JSFIDLE无法处理弹出窗口。
<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>