javascript - 如何从此 javascript 代码中删除复选框
问题描述
以下脚本的功能是从网页调用“Android Share”选项。我想删除复选框,我该怎么做?我尝试删除 html 行,但是代码不起作用。只有磁贴、文本和 URL 字段是必需的。没有复选框和文件上传选项。
<table>
<tr><td>Title:</td>
<td><input type="checkbox" id="title_checkbox" checked/></td>
<td><input id="title" value="The Title" size="40" /></td>
</tr>
<tr><td>Text:</td>
<td><input type="checkbox" id="text_checkbox" checked/></td>
<td><input id="text" value="The message" size="40"/></td>
</tr>
<tr><td>URL:</td>
<td><input type="checkbox" id="url_checkbox" checked/></td>
<td><input id="url" value="https://example.com" size="40"/></td>
</tr>
<tr><td>Files:</td>
<td><!--Without column 2, the whole table is small on mobile.--></td>
<td><input id="files" type="file" multiple></td>
</tr>
</table>
<p><input id="share" type="button" value="Share" />
<input id="share-no-gesture" type="button" value="Share without user gesture" /></p>
<div id="output"></div>
这是其余的代码,javascript
<script>
'use strict';
function sleep(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
function logText(message, isError) {
if (isError)
console.error(message);
else
console.log(message);
const p = document.createElement('p');
if (isError)
p.setAttribute('class', 'error');
document.querySelector('#output').appendChild(p);
p.appendChild(document.createTextNode(message));
}
function logError(message) {
logText(message, true);
}
function checkboxChanged(e) {
const checkbox = e.target;
const textfield = document.querySelector('#' + checkbox.id.split('_')[0]);
textfield.disabled = !checkbox.checked;
if (!checkbox.checked)
textfield.value = '';
}
async function testWebShare() {
if (navigator.share === undefined) {
logError('Error: Unsupported feature: navigator.share()');
return;
}
const title_input = document.querySelector('#title');
const text_input = document.querySelector('#text');
const url_input = document.querySelector('#url');
const file_input = document.querySelector('#files');
const title = title_input.disabled ? undefined : title_input.value;
const text = text_input.disabled ? undefined : text_input.value;
const url = url_input.disabled ? undefined : url_input.value;
const files = file_input.disabled ? undefined : file_input.files;
if (files && files.length > 0) {
if (!navigator.canShare || !navigator.canShare({files})) {
logError('Error: Unsupported feature: navigator.canShare()');
return;
}
}
try {
await navigator.share({files, title, text, url});
logText('Successfully sent share');
} catch (error) {
logError('Error sharing: ' + error);
}
}
async function testWebShareDelay() {
await sleep(6000);
testWebShare();
}
function onLoad() {
// Checkboxes disable and delete textfields.
document.querySelector('#title_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#text_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#url_checkbox').addEventListener('click',
checkboxChanged);
document.querySelector('#share').addEventListener('click', testWebShare);
document.querySelector('#share-no-gesture').addEventListener('click',
testWebShareDelay);
if (navigator.share === undefined) {
if (window.location.protocol === 'http:') {
// navigator.share() is only available in secure contexts.
window.location.replace(window.location.href.replace(/^http:/, 'https:'));
} else {
logError('Error: You need to use a browser that supports this draft ' +
'proposal.');
}
}
}
window.addEventListener('load', onLoad);
</script>
</body>
</html>
解决方案
你可以这样修复。您还必须删除相关的 javascript 代码。
<table>
<tr><td>Title:</td>
<td><input id="title" value="The Title" size="40" /></td>
</tr>
<tr><td>Text:</td>
<td><input id="text" value="The message" size="40"/></td>
</tr>
<tr><td>URL:</td>
<td><input id="url" value="https://example.com" size="40"/></td>
</tr>
</table>
<p><input id="share" type="button" value="Share" />
<input id="share-no-gesture" type="button" value="Share without user gesture" /></p>
<div id="output"></div>
function sleep(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
function logText(message, isError) {
if (isError)
console.error(message);
else
console.log(message);
const p = document.createElement('p');
if (isError)
p.setAttribute('class', 'error');
document.querySelector('#output').appendChild(p);
p.appendChild(document.createTextNode(message));
}
function logError(message) {
logText(message, true);
}
async function testWebShare() {
if (navigator.share === undefined) {
logError('Error: Unsupported feature: navigator.share()');
return;
}
const title_input = document.querySelector('#title');
const text_input = document.querySelector('#text');
const url_input = document.querySelector('#url');
const title = title_input.disabled ? undefined : title_input.value;
const text = text_input.disabled ? undefined : text_input.value;
const url = url_input.disabled ? undefined : url_input.value;
try {
await navigator.share({title, text, url});
logText('Successfully sent share');
} catch (error) {
logError('Error sharing: ' + error);
}
}
async function testWebShareDelay() {
await sleep(6000);
testWebShare();
}
function onLoad() {
document.querySelector('#share').addEventListener('click', testWebShare);
document.querySelector('#share-no-gesture').addEventListener('click',
testWebShareDelay);
if (navigator.share === undefined) {
if (window.location.protocol === 'http:') {
// navigator.share() is only available in secure contexts.
window.location.replace(window.location.href.replace(/^http:/, 'https:'));
} else {
logError('Error: You need to use a browser that supports this draft ' +
'proposal.');
}
}
}
window.addEventListener('load', onLoad);
推荐阅读
- reporting-services - 如何在批处理模式下调用输出菜单项并执行报表?
- c++ - QMouseEvent 用于 QWidget 上的单次移动
- python - 如何赋予价值并自动将其添加(增加)到其他变量,同时减少列表python中的另一个?
- jquery - 如果引导模式框单击无按钮,如何丢弃复选框值并显示从数据库获取的值
- database - MongoDB是否有可能立即在磁盘上写入而不将信息存储在内存中?
- sql - ts_vector 按月分组
- hybris - SAP HYBRIS [Y]:如何限制 customersupportagentrole 用户取消或退回订单
- java - 如何通过注释用 try-catch 包装方法?
- php - 如何转换 Swift 日期和 TimeInterval 以便我可以发送到 php 服务器?
- angular - 特定 STEP 角度底漆的样式