import {check,parse,textFile,xmlFile,put,done,text,tableCD,xhr} from './xml-browser.js';
try{
const doc=await xmlFile('cd_catalog.xml'),items=[...doc.getElementsByTagName('CD')],previous=document.querySelector('#previous'),next=document.querySelector('#next');let index=0;function show(){check(index>=0&&index<items.length,'index bounds');put('#position',(index+1)+'/'+items.length);put('#detail',JSON.stringify({artist:text(items[index],'ARTIST'),title:text(items[index],'TITLE'),year:text(items[index],'YEAR')},null,2));previous.disabled=index===0;next.disabled=index===items.length-1;for(const button of document.querySelectorAll('[data-index]'))button.setAttribute('aria-pressed',String(Number(button.dataset.index)===index));window.mbskyIndex=index}previous.onclick=()=>{if(index>0){index--;show()}};next.onclick=()=>{if(index<items.length-1){index++;show()}};items.forEach((cd,i)=>{const button=document.createElement('button');button.dataset.index=String(i);button.textContent=text(cd,'TITLE');button.onclick=()=>{index=i;show()};document.querySelector('#choices').append(button)});show();done({initial_index:index,records:items.length,previous_initial_disabled:previous.disabled,first_title:text(items[0],'TITLE')});
}catch(error){window.mbskyError=String(error);document.querySelector('#status').textContent='运行失败:'+String(error)}
本例需在 Windows / Python3.12+lxml6.1.3 / Edge156 / 自有本地HTTP,PHP为独立附例 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / Python3.12+lxml6.1.3 / Edge156 / 自有本地HTTP,PHP为独立附例
入口文件:projects/xml-lab/xml-applications.js
预期结果与验证条件观察提示
- 初次索引0,连续next止于2,previous止于0,点击第二行更新详情/aria-pressed/按钮状态。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202611a539.zip