javascript中的document.getDocumentById返还null

1. 背景

我想做一个起点自动投推荐票的firefox插件,希望插件自己点击投推荐票dialog中的提交button,

但是我使用document.getDocumentById一直返回的是null,然后我就查找原因。

2. 最后发现投推荐票的dialog是用iframe实现的,但是当前的页面属于另外一个iframe,它的document中根本没有提交button的id(btn_OK)。

所以只能对当前所有的frame进行遍历,一个一个的找,如果找到的话调用click方法就好了,以下是方法的原型:

var targetId = "btn_OK"
var target = document.getElementById(targetId);
if(target != null) {
    //do something
}
var iframes = document.getElementsByTagName('iframe'); //all iframes on page
console.log("total frame " + iframes.length);
var errCount = 0;
for(var i=0; i<iframes.length; i++){
    console.log("current frame is: " + iframes[i]);
    var iframeDocument;
    try {
	    iframeDocument = iframes[i].contentDocument || iframes[i].contentWindow.document;
		target = iframeDocument.getElementById(targetId);
		console.log(target);
		}
	catch (err){
	    errCount ++;
	    console.log(err);
	}	
}
因为不同的iframe有可能是不同的domain的,所有需要try catch。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class DocumentService extends ServiceImpl<DocumentMapper, Document> { @Resource private DocumentMapper documentMapper; @Resource private CateMapper cateMapper; @Resource private HistoryMapper historyMapper; public List<Document> listDocument(PageInfo<?> pageInfo, Document document) { PageHelper.startPage(pageInfo.getCurrent(), pageInfo.getPageSize()); List<Document> documentList = documentMapper.selectList(Wrappers.<Document>lambdaQuery() .like(StringUtils.isNotBlank(document.getTitle()), Document::getTitle, document.getTitle()) .like(StringUtils.isNotBlank(document.getSummary()), Document::getSummary, document.getSummary()) .eq(document.getCateId() != null, Document::getCateId, document.getCateId()) .eq(document.getDeptId() != null, Document::getDeptId, document.getDeptId()) .eq(document.getActive() != null, Document::getActive, document.getActive()) .eq(document.getRecommend() != null, Document::getRecommend, document.getRecommend()) .eq(document.getCarousel() != null, Document::getCarousel, document.getCarousel()) .orderByDesc(Document::getDateTime) ); if (!documentList.isEmpty()) { List<Long> documentIdList = documentList.stream().map(Document::getCateId).collect(Collectors.toList()); Map<Long, Cate> cateMap = cateMapper.selectBatchIds(documentIdList).stream().collect(Collectors.toMap(Cate::getId, e -> e, (e1, e2) -> e2)); for (Document documentItem : documentList) { documentItem.setCate(cateMap.get(documentItem.getCateId())); } } return documentList; } public Document getDocumentById(Long id, HttpServletRequest request) { Document document = documentMapper.selectById(id); if (document == null) { return null; } document.setHits(document.getHits() + 1); documentMapper.updateById(document); document.setCate(cateMapper.selectById(document.getCateId())); History history = new History(); history.setDocumentId(id); history.setDateTime(LocalDateTime.now()); history.setUserId(Long.valueOf((String) StpUtil.getLoginId())); history.setIp(request.getRemoteAddr()); historyMapper.insert(history); return document; } public List<Document> listRecommend(Long id) { //Todo 推荐 return new ArrayList<>(); } public Boolean copyDocument(Long id) { Document document = documentMapper.selectById(id); if (document == null) { return false; } document.setDateTime(LocalDateTime.now()); document.setId(null); document.setDeptId(StpUtil.getSession().getModel(SaSession.USER, User.class).getBelongDeptId()); document.setHits(0L); document.setActive(false); document.setRecommend(false); int result = documentMapper.insert(document); return result == 1; } }
最新发布
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值