在插入符号位置的iframe中插入图片

时间:2019-01-08 10:38:43

标签: javascript html createobject

用于富文本框。我想在插入符的位置插入用户选择的图片。

首先我尝试过:

var loadImage = function(event) {
    var editor = editFrame.document;
    editor.execCommand("insertImage", false, event.target.files[0] );
};

在我的iframe中,当前位置上只有一个断开的链接图标但是。没有图像。然后有人告诉我尝试:

var loadImage = function(event) {
    var editor  =  document.getElementById('editFrame');
    editor.src = URL.createObjectURL(event.target.files[0]);
};

这不是我想要的,但是,在这种情况下,我没有断开链接的问题,并且图像可以正常加载。

我如何才能获得两个世界中最好的? :)

有人可以解释一下如何在期望的位置加载图像吗?在插入符号位置?我读了一些东西,但是作为一个初学者,这一切在我脑海中都很混乱。

仅使用 JavaScript,好吗?我的意思是@David Bray提供的解决方案效果很好。但是它使用了JQuery,我不知道它在做什么……或者有人可以将JQuery转换为Javascript?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

这是David Bray脚本的纯JavaScript版本,并进行了其他各种更改。它与他的答案足够接近,以至于认为它只是一个不同的版本,但与仅编辑原始答案相差太大。高亮显示的另一项功能是在加载图像后将输入元素值设置为空。如果不这样做,则删除图像后将无法重新加载该图像。

// filter_node.cpp
// Does, say, stereo fusion on time-synced images to make pointcloud

// ROS
#include <ros/ros.h>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>

// ROS Msgs
#include <sensor_msgs/CameraInfo.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/PointCloud2.h>

// Std Libs
#include <string>
#include <vector>

// Custom Lib
#include "my_new_pkg/my_math.h"

// Subscribers (inputs)
//    cam_left_info_sub (sensor_msgs/CameraInfo): "camera_left/camera_info"
//    cam_left_sub (sensor_msgs/Image): "camera_left/image_raw"
//    cam_right_info_sub (sensor_msgs/CameraInfo): "camera_right/camera_info"
//    cam_right_sub (sensor_msgs/Image): "camera_right/image_raw"

// Publishers (outputs)
//    pcl_pub (sensor_msgs/PointCloud2): "camera/pointcloud"

// Parameters (settings)
//    frequency (double): default= 20.0 Hz
//    debug (bool): default= false

// ROS Handles and Publishers
ros::NodeHandle *nh;
ros::NodeHandle *pnh;
ros::Publisher pcl_pub;

// ROS Callbacks
void cam_left_cb(const sensor_msgs::CameraInfo::ConstPtr& pcinfo_msg);
void cam_right_cb(const sensor_msgs::CameraInfo::ConstPtr& pcinfo_msg);
void timer_cb(const ros::TimerEvent&);
void cam_cb(
  const sensor_msgs::Image::ConstPtr& pcleft_msg,
  const sensor_msgs::Image::ConstPtr& pcright_msg
);

// Params
double frequency = 20.0;
bool debug = false;

// Global Variables
sensor_msgs::PointCloud2 pcl_msg;
sensor_msgs::CameraInfo cinfo_left_msg;
sensor_msgs::CameraInfo cinfo_right_msg;
sensor_msgs::Image cleft_msg;
sensor_msgs::Image cright_msg;

int main(int argc, char **argv){
// Init ROS
  ros::init(argc, argv, "filter_node");
  nh = new ros::NodeHandle(); // Public namespace nh
  pnh = new ros::NodeHandle("~"); // Private namespace nh

// Params and Init
  pnh->getParam("frequency", frequency);
  pnh->getParam("debug", debug);

// Subscribers
  // Notice, normal subscribers:
  ros::Subscriber cam_left_info_sub = nh->subscribe("camera_left/camera_info", 1, cam_left_cb);
  ros::Subscriber cam_right_info_sub = nh->subscribe("camera_right/camera_info", 1, cam_right_cb);
  ros::Timer timer = nh->createTimer(ros::Duration(1.0/frequency), timer_cb);
  // message_filters subscribers
  message_filters::Subscriber<sensor_msgs::Image> cam_left_sub(*nh, "camera_left/image_raw", 5);
  message_filters::Subscriber<sensor_msgs::Image> cam_right_sub(*nh, "camera_right/image_raw", 5);
  // This is where the magic happens, then the callback gets registered
  message_filters::TimeSynchronizer <sensor_msgs::Image, sensor_msgs::Image> cam_sync(
    cam_left_sub, cam_right_sub, 1); // 1 here means the most recent sync'd img pair
  cam_sync.registerCallback(boost::bind(&cam_cb, _1, _2)); // Magic: _X up to num subs

// Publishers
  pcl_pub = nh->advertise<sensor_msgs::PointCloud2>("camera/pointcloud", 1);

// Spin & Cleanup
  ros::spin();
  delete nh;
  delete pnh;
  return 0;
}

// This is ineff on big data structures
void cam_left_cb(const sensor_msgs::CameraInfo::ConstPtr& pcinfo_msg){
  cinfo_left_msg = *pcinfo_msg;
}

void cam_right_cb(const sensor_msgs::CameraInfo::ConstPtr& pcinfo_msg){
  cinfo_right_msg = *pcinfo_msg;
}

// This callback will be called whenever they're sync'd up
// You can choose to compute the data right away or store it
// for a more refined computation rate
void cam_cb(
  const sensor_msgs::Image::ConstPtr& pcleft_msg,
  const sensor_msgs::Image::ConstPtr& pcright_msg
){
  cleft_msg = *pcleft_msg;
  cright_msg = *pcright_msg;
}

void timer_cb(const ros::TimerEvent&){
  // call math routine
  // my_math(&cleft_msg.data, &cright_msg.data, &pcl_msg.data);
  // Fill pcl_msg
  pcl_pub.publish(pcl_msg);
}