从单独的html文件预览Javascript文本
Javascript text preview from separate html file
我想将另一个(news.html)页面中的文本预览加载到主页中的div(id="news_pre")中。我目前正在使用getElementById加载中的news.html页面,它正在运行。然而,我只想预览news.html页面,最大字符数(150)。我知道这可以在php中完成,但我不知道如何使用php,我想知道是否有一种方法可以在javascript中完成。谢谢,这是我的密码。
<div id="news_pre">
<p> news </p>
<script>
function news(){
document.getElementById("news_pre").innerHTML='<object width="100%" height="100%" type="text/html" data="news.html" ></object>';
}
</script>
</div>
我建议,如果您想操作来自另一个页面的数据,而不是使用对象标记,那么您可以使用AJAX请求。如果你想要框架式的外观,你可以随时使用css应用一些边框样式。
<!DOCTYPE html>
<html>
<script>
window.onload = function() {
// when the window loads
var xhr; (XMLHttpRequest) ? xhr= new XMLHttpRequest() : xhr= new ActiveXObject("Microsoft.XMLHTTP");
//check if XMLHttpRequest is available in browser, if IE use ActiveXObject instead.
xhr.onload = function() {
// when loading the request do the following
if(xhr.readyState === 4 && xhr.status === 200) {
// if the load is completed successfully
var preview = xhr.responseText;
// Get news.html and place it in the variable preview
preview = preview.substring(0, 149);
// Keep the first 150 characters from news.html in the variable preview
document.getElementById("content").innerHTML = preview;
// Place the content in preview into the element with id "content"
}
}
xhr.open("GET", "news.html", true);
//This tells the request where to go
xhr.send();
//This sends the request to get news.html
}
</script>
<h1> News Preview is Below! </h1>
<p>
<section id="content"></section> <br>
</html>
编辑:如果您需要更多信息,可以查看此XMLHttp教程
相关文章:
- 从单独的html文件预览Javascript文本
- Javascript文本框验证不起作用
- 使用TreeWalker检索非Javascript文本节点
- Javascript文本框-查找最低的输入和填充
- javascript文本验证不起作用.警报消息没有't显示
- JavaScript 文本节点属性
- 简单的Javascript文本编辑器或一组函数
- 将光标放在 javascript 文本框的末尾
- JavaScript 文本区域值,带有数据库中的新行
- 从动态创建的javascript文本框中获取文本输入
- 为什么这个使用谷歌URL的Javascript文本到语音不起作用
- 如何在Javascript文本区域中捕获事件以记录所有键入的文本
- JavaScript文本转换
- 我正在尝试将javascript文本框的输入保存回服务器.我想从保存的弹出文本框中获得输入,以重命名文件夹
- JavaScript文本游戏积分系统
- 基于编辑距离的javascript文本相似度百分比算法
- 如何在React.NET中导出JavaScript文本
- 用于javascript文本输入的简单-+按钮
- asp.net c#中的javascript文本框验证
- 从javascript文本模板访问HTML类