首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用普通JavaScript的HTML模板

使用普通JavaScript的HTML模板
EN

Stack Overflow用户
提问于 2018-09-24 23:27:06
回答 2查看 403关注 0票数 2

我有一个SharePoint应用程序,可以从库中检索页面内容,并为桌面和移动站点提供数据。我们在页面中也有链接,我如何用普通的JavaScript呈现HTML模板?

例如,我们有两个来自页面内容的页面链接:

<a class="template" href="/3.aspx">

我想添加的href前面的http://<sitename>/pages/http://<sitename>/pages/m/取决于屏幕大小或设备。

我如何通过vanilla JS实现这一点?

EN

回答 2

Stack Overflow用户

发布于 2018-09-25 04:56:31

有了一个名为lit-html的轻量级库,事情就简单了:

代码语言:javascript
复制
import { html, render } from 'https://unpkg.com/lit-html/lit-html.js?module';

const linkTemplate = orientation =>
  html`<a href="https://<sitename>/pages/{orientation != null ? 'm/' : '/'}3.aspx">Link text</a>`

render(linkTemplate(window.orientation), container)
票数 2
EN

Stack Overflow用户

发布于 2018-09-25 02:04:56

屏幕大小方式:

代码语言:javascript
复制
function isMobile() {
    return window.innerWidth < 800;
}

var mobileUrl = 'http://<sitename>/pages/m/1.html';
var desktopUrl = 'http://<sitename>/pages/index.html';
window.location = isMobile() ? mobileUrl : desktopUrl;

更优雅的“鸭式”移动支票

代码语言:javascript
复制
function isMobile() {
    return typeof window.orientation !== 'undefined';
}

还有一种分析navigator.userAgent值的方法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52482642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档