js图片解析工具类

在开发的过程中作为后端开发人员的我们有时候总是免不了被当成半个前端人员使用,所以我就总结了一下js图片解析的工具类

代码

function getImgViewByURL(urlstr) {
  if (urlstr) {
    let url = [];
    try {
      url = JSON.parse(urlstr);
    } catch (e) {
      console.log('图片解析异常:',e);
      return null;
    }
    const dd = url.map((item, index) =>
         <img width="90px" height="90px" onClick={() =>window.open(item)}  style={{  border: '1px solid #ccc',cursor: 'pointer', marginRight: '3px'}} src={item} key={index} />);
    return dd;
  }
  return null;
}

注意:

  1. 传入的urlStr一定要是json数组,如果不是json串,可以使用JSON.stringify(urlStr)转换一下。
  2. 如果传入的不是json数组,而是一个字符串,那么可以使用js的new Array(urlStr)方法变成数组,然后采用JSON.stringify(urlStr)方法改变一下。

猜你喜欢

转载自blog.csdn.net/y12nre/article/details/77678715
今日推荐