Discuz 3.X 门户文章插入图片自动添加 alt 标签

最近用 Discuz 搭建了个网站——儿童安全座椅网(www.bbseat.com.cn),用到了门户功能,不得不说Discuz 的功能还是非常强大的,但在使用过程中发现在发表文章时添加了图片却不能像 WordPress 这样自动添加 alt 标签,经过一番研究,初步解决了这个问题,目前还没有 Bug,等待长时间验证,方法如下:
在实施本方法之前请先备份网站数据,以防不测;这次修改需要修改两个文件,分别是:

static/image/editor/editor_function.js
template/default/home/spacecp_blog.htm

同时增加一个文件为:

static/image/editor/editor_function.js–复制一份,重命名为:bgeditor_function.js

下面开始修改:editor_function.js

查找代码:

function insertImage(image, url, width, height) {
    url = typeof url == 'undefined' || url === null ? image : url;
    width = typeof width == 'undefined' || width === null ? 0 : parseInt(width);
    height = typeof height == 'undefined' || height === null ? 0 : parseInt(height);
    var html = '<p><a href="' + url + '" target="_blank"><img src="'+image+'"'+(width?' width="'+width+'"':'')+(height?' height="'+height+'"':'')+'></a></p>';
    edit_insert(html);
}

修改为:

function insertImage(image, url, width, height, subject) {
    url = typeof url == 'undefined' || url === null ? image : url;
    width = typeof width == 'undefined' || width === null ? 0 : parseInt(width);
    height = typeof height == 'undefined' || height === null ? 0 : parseInt(height);
    subject = $('title').value;
    var html = '<p><a href="' + url + '" target="_blank"><img alt="'+subject+'" src="'+image+'"'+(width?' width="'+width+'"':'')+(height?' height="'+height+'"':'')+'></a></p>';
    edit_insert(html);
}

再修改刚刚建立的bgeditor_function.js

同样查找代码:

function insertImage(image, url, width, height) {
    url = typeof url == 'undefined' || url === null ? image : url;
    width = typeof width == 'undefined' || width === null ? 0 : parseInt(width);
    height = typeof height == 'undefined' || height === null ? 0 : parseInt(height);
    var html = '<p><a href="' + url + '" target="_blank"><img src="'+image+'"'+(width?' width="'+width+'"':'')+(height?' height="'+height+'"':'')+'></a></p>';
    edit_insert(html);
}

修改为:

function insertImage(image, url, width, height, subject) {
    url = typeof url == 'undefined' || url === null ? image : url;
    width = typeof width == 'undefined' || width === null ? 0 : parseInt(width);
    height = typeof height == 'undefined' || height === null ? 0 : parseInt(height);
    var html = '<p><a href="' + url + '" target="_blank"><img alt="'+subject+'" src="'+image+'"'+(width?' width="'+width+'"':'')+(height?' height="'+height+'"':'')+'></a></p>';
    edit_insert(html);
}

再编辑:template/default/home/spacecp_blog.htm

查找:editor_function.js

替换为:bgeditor_function.js

编辑完毕保存,把文件上传到原来位置,增加的bgeditor_function.js与editor_function.js一块放在同一目录。后台发布一片文章,输入文章标题,上传图片,进入源代码模式,看看图片是不是有了 alt 标签?!

原文地址:https://www.cnblogs.com/kimshen/p/6026032.html