概述
本文用作个人备份,记录一些有用的小函数
计算文本DOM渲染到页面的实际宽度
function getTextWidth(text, fontSize) {
const span = document.createElement("span");
span.style.display = "inline-block";
span.style.width = "auto";
span.style.whiteSpace = "nowrap";
span.style.fontSize = fontSize;
span.textContent = text;
document.body.appendChild(span);
const width = span.offsetWidth;
document.body.removeChild(span);
return width;
}
在Vue中获取客户端ip
methods:{
ipCallback(data){
// ...这里写处理逻辑
}
},
mounted() {
// 利用jsonp向外部接口获取客户端的外网ip地址
const script = document.createElement("script");
script.src = "https://api.ipify.org?format=jsonp&callback=ipCallback";
// 因为jsonp是通过script标签的src属性来请求数据的,所以需要将回调函数挂载到window上
window.ipCallback = this.ipCallback.bind(this);
document.body.appendChild(script);
},
最后
以上就是懵懂白开水为你收集整理的有用的小函数:计算文本DOM渲染到页面的宽度、在Vue中获取客户端ip的全部内容,希望文章能够帮你解决有用的小函数:计算文本DOM渲染到页面的宽度、在Vue中获取客户端ip所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复