<input type="text"> 的document.getElementById("").value和document.getElementById("").getAttribute("val...

本文详细解析了一个用于处理产品属性的脚本,包括产品属性的类型、名称、编码及附加属性的设置,通过HTML元素和JavaScript实现动态生成和操作表格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <!--产品属性处理部分开始-->
               <table id="productAttributeBlock" class="tabEditList" style="width:100%;">
                    <thead>
                        <tr>
                            <th>
                                属性类型
                            </th>
                            <th>
                                属性名称
                            </th>
                            <th>
                                属性编码
                            </th>
                            <th>
                                是否附加属性
                            </th>
                            <th>
                                操作
                            </th>
                        </tr>
                    </thead>
               </table>
               <script type="text/javascript">

                   var Init = function () {
                       var oTBody = document.createElement('TBODY');
                       document.getElementById('productAttributeBlock').appendChild(oTBody);
                   };

                   var AddRow = function (isAdd) {
                       var rowIndex = document.getElementById('productAttributeBlock').tBodies[0].rows.length;
                       var oRow = document.createElement('TR');
                       oRow.setAttribute("id", "row" + rowIndex);
                       var oUnit = document.createElement('TD');
                       oUnit.innerHTML = '<input type="text" id="type' + rowIndex + '" name="attributeType"/>';
                       oRow.appendChild(oUnit);
                       oUnit = document.createElement('TD');
                       oUnit.innerHTML = '<input type="text" id="name' + rowIndex + '" name="attributeName"/>';
                       oRow.appendChild(oUnit);
                       oUnit = document.createElement('TD');
                       oUnit.innerHTML = '<input type="text" id="code' + rowIndex + '" name="attributeCode"/>';
                       oRow.appendChild(oUnit);
                       oUnit = document.createElement('TD');
                       oUnit.innerHTML = '<input type="text" id="additional' + rowIndex + '" name="isAdditional"/>';
                       oRow.appendChild(oUnit);
                       oUnit = document.createElement('TD');
                       oUnit.innerHTML = '<input type="button" value="添加" id="add' + rowIndex + '" onclick="addFunc()"/>';
                       oRow.appendChild(oUnit);
                       return oRow;
                   };

                   var addFunc = function () {
                       //alert(document.getElementById("type0").value);
                       alert(document.getElementById("type0").getAttribute("value"));
                   };

                   Init();
                   //AddRow();
                   if (document.getElementById('productAttributeBlock').tBodies[0].rows.length===0)
                   {
                    document.getElementById('productAttributeBlock').tBodies[0].appendChild(AddRow());
                   }
                   
               </script>
               <!--产品属性处理结束-->

<input type="text"> 的document.getElementById("").value和document.getElementById("").getAttribute("value")不相同:

document.getElementById("").value得到输入值

document.getElementById("").getAttribute("value")得到null

转载于:https://www.cnblogs.com/hongjiumu/archive/2012/09/06/2673968.html

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Mermaid 编辑器(增强版)</title> <style> :root { --font-main: -apple-system, BlinkMacSystemFont, 'San Francisco', Helvetica, Arial, sans-serif; --primary-color: #007aff; --text-color: #111; --bg-color: #f9f9f9; --card-bg: #fff; --border-radius: 20px; --shadow: 0 4px 12px rgba(0, 0, 0, 0.1); } body { font-family: var(--font-main); background: var(--bg-color); color: var(--text-color); margin: 0; padding: 2rem; } .container { max-width: 960px; margin: auto; } .card { background: var(--card-bg); border-radius: var(--border-radius); padding: 2rem; margin-bottom: 2rem; box-shadow: var(--shadow); } textarea { width: 100%; height: 200px; font-family: monospace; border-radius: 10px; padding: 1rem; margin-bottom: 1rem; border: 1px solid #ccc; } button, select { font-family: inherit; background: var(--primary-color); color: white; padding: 0.5rem 1rem; border: none; border-radius: 8px; cursor: pointer; margin-right: 10px; } .mermaid-box { background: white; border-radius: 12px; padding: 1rem; overflow-x: auto; } </style> <script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/canvg/dist/browser/canvg.min.js"></script> </head> <body> <div class="container"> <div class="card"> <h2>🛠 自定义 Mermaid 编辑器(增强)</h2> <textarea id="mermaidInput">graph TD\n A[开始] --> B[步骤一]\n B --> C[步骤二]\n C --> D[结束]</textarea> <div> <button onclick="renderMermaid()">渲染图表</button> <button onclick="saveSnapshot()">保存快照</button> <button onclick="exportPNG()">导出 PNG</button> <button onclick="clearMermaid()">清空</button> <select id="themeSelect" onchange="switchTheme(this.value)"> <option value="default">亮色主题</option> <option value="dark">暗色主题</option> <option value="neutral">中性灰</option> </select> </div> <div class="mermaid-box" id="customMermaid"></div> <h3>📜 历史快照</h3> <ul id="historyList"></ul> </div> </div> <script> let theme = 'default'; let history = JSON.parse(localStorage.getItem('mermaidHistory') || '[]'); let currentCode = localStorage.getItem('lastCode') || document.getElementById('mermaidInput').value; function switchTheme(val) { theme = val; renderMermaid(); } function renderMermaid() { const code = document.getElementById('mermaidInput').value; currentCode = code; localStorage.setItem('lastCode', code); const insert = document.getElementById('customMermaid'); insert.innerHTML = ''; mermaid.initialize({ startOnLoad: false, theme }); mermaid.render('graphDiv', code, (svgCode) => { insert.innerHTML = svgCode; }); } function clearMermaid() { document.getElementById('mermaidInput').value = ''; document.getElementById('customMermaid').innerHTML = ''; localStorage.removeItem('lastCode'); } function saveSnapshot() { history.unshift(currentCode); if (history.length > 10) history = history.slice(0, 10); localStorage.setItem('mermaidHistory', JSON.stringify(history)); loadHistory(); } function loadHistory() { const list = document.getElementById('historyList'); list.innerHTML = ''; history.forEach((code, i) => { const li = document.createElement('li'); li.innerHTML = `<a href="#" onclick='restore(${i})'>图 ${i + 1}</a>`; list.appendChild(li); }); } function restore(index) { const code = history[index]; document.getElementById('mermaidInput').value = code; renderMermaid(); } async function exportPNG() { const svg = document.querySelector('#customMermaid svg'); if (!svg) return alert('请先渲染图表'); const canvas = document.createElement('canvas'); canvas.width = 1024; canvas.height = 768; const ctx = canvas.getContext('2d'); const v = await Canvg.fromString(ctx, new XMLSerializer().serializeToString(svg)); await v.render(); const link = document.createElement('a'); link.download = 'diagram.png'; link.href = canvas.toDataURL(); link.click(); } // 初始化渲染 document.getElementById('mermaidInput').value = currentCode; renderMermaid(); loadHistory(); </script> </body> </html> 上面的代码有问题,无法渲染mermaid,无法生成渲染图,
07-16
<template> <div :class="prefixCls" :style="{ width: containerWidth }"> <ImgUpload :fullscreen="fullscreen" @uploading="handleImageUploading" @done="handleDone" @insert="handleInsertImg" v-if="showImageUpload" v-show="editorRef" :disabled="disabled" /> <textarea :id="tinymceId" ref="elRef" :style="{ visibility: 'hidden' }" v-if="!initOptions.inline"></textarea> <Sqltext :visible="SqltextOpen" :businessType="businessType" :textreavalue="value" v-if="SqltextOpen" @close="CloseSqltext" /> <Sqltable :visible="SqltableOpen" :businessType="businessType" :textreavalue="value" v-if="SqltableOpen" @close="CloseSqltable" /> <slot v-else></slot> </div> </template> <script lang="ts"> import type { Editor, RawEditorSettings } from 'tinymce'; import tinymce from 'tinymce/tinymce'; import 'tinymce/themes/silver'; import 'tinymce/icons/default/icons'; import 'tinymce/plugins/advlist'; import 'tinymce/plugins/anchor'; import 'tinymce/plugins/autolink'; import 'tinymce/plugins/autosave'; import 'tinymce/plugins/code'; import 'tinymce/plugins/codesample'; import 'tinymce/plugins/directionality'; import 'tinymce/plugins/fullscreen'; import 'tinymce/plugins/hr'; import 'tinymce/plugins/insertdatetime'; import 'tinymce/plugins/link'; import 'tinymce/plugins/lists'; import 'tinymce/plugins/media'; import 'tinymce/plugins/nonbreaking'; import 'tinymce/plugins/noneditable'; import 'tinymce/plugins/pagebreak'; import 'tinymce/plugins/paste'; import 'tinymce/plugins/preview'; import 'tinymce/plugins/print'; import 'tinymce/plugins/save'; import 'tinymce/plugins/searchreplace'; import 'tinymce/plugins/spellchecker'; import 'tinymce/plugins/tabfocus'; import 'tinymce/plugins/table'; import 'tinymce/plugins/template'; import 'tinymce/plugins/textpattern'; import 'tinymce/plugins/visualblocks'; import 'tinymce/plugins/visualchars'; import 'tinymce/plugins/wordcount'; import 'tinymce/plugins/image'; import 'tinymce/plugins/charmap'; import 'tinymce/plugins/imagetools'; import 'tinymce/plugins/help'; import 'tinymce/plugins/quickbars'; import 'tinymce/plugins/importcss'; import 'tinymce/plugins/toc'; import 'tinymce/plugins/emoticons'; import { defineComponent, computed, nextTick, ref, unref, watch, onDeactivated, onBeforeUnmount, onMounted } from 'vue'; import ImgUpload from './ImgUpload.vue'; import { toolbar, plugins } from './tinymce'; import { buildShortUUID } from '@/utils/uuid'; import { bindHandlers } from './helper'; import { onMountedOrActivated } from '@/hooks/core/onMountedOrActivated'; import { useDesign } from '@/hooks/web/useDesign'; import { isNumber } from '@/utils/is'; import { useLocale } from '@/locales/useLocale'; import { useAppStore } from '@/store/modules/app'; import { useDebounceFn } from '@vueuse/core'; import Sqltext from './Sqltext.vue'; import Sqltable from './Sqltable.vue'; import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n(); const tinymceProps = { options: { type: Object as PropType<Partial<RawEditorSettings>>, default: () => ({}), }, value: { type: [String, Array, Object], }, placeholder: { type: String, default: '请输入', }, toolbar: { type: [String, Array] as PropType<string | string[]>, default: toolbar, }, plugins: { type: [String, Array] as PropType<string | string[]>, default: plugins, }, height: { type: [Number, String] as PropType<string | number>, required: false, default: 400, }, width: { type: [Number, String] as PropType<string | number>, required: false, default: 'auto', }, showImageUpload: { type: Boolean, default: true, }, businessType: { // 模板类型 type: [Number, String, undefined] as PropType<string | Number | undefined>, default: undefined, }, }; export default defineComponent({ name: 'JnpfEditor', components: { ImgUpload, Sqltext, Sqltable }, inheritAttrs: false, props: tinymceProps, emits: ['change', 'update:value', 'inited', 'init-error', 'checkboxeditor', 'checkitemeditor'], setup(props, { emit, attrs }) { const editorRef = ref<Nullable<Editor>>(null); const fullscreen = ref(false); const SqltextOpen = ref(false); const SqltableOpen = ref(false); const tinymceId = ref<string>(buildShortUUID('tiny-vue')); const elRef = ref<Nullable<HTMLElement>>(null); const business = ref(null); const { prefixCls } = useDesign('tinymce-container'); const appStore = useAppStore(); const debounceUpdateEditor = useDebounceFn(updateEditor, 300); const tinymceContent = computed(() => props.value); const containerWidth = computed(() => { const width = props.width; if (isNumber(width)) { return `${width}px`; } return width; }); const skinName = computed(() => { return appStore.getDarkMode === 'light' ? 'oxide' : 'oxide-dark'; }); const langName = computed(() => { const lang = useLocale().getLocale.value; return ['zh_CN', 'en_US'].includes(lang) ? lang : 'zh_CN'; }); const initOptions = computed((): RawEditorSettings => { const { height, options, toolbar, plugins, placeholder, businessType } = props; const publicPath = import.meta.env.VITE_PUBLIC_PATH || '/'; return { selector: `#${unref(tinymceId)}`, height, toolbar, menubar: 'file edit insert view format table', plugins, language_url: publicPath + 'resource/tinymce/langs/' + langName.value + '.js', language: langName.value, branding: false, default_link_target: '_blank', link_title: false, object_resizing: true, paste_data_images: true, // 允许粘贴图像 auto_focus: undefined, skin: skinName.value, skin_url: publicPath + 'resource/tinymce/skins/ui/' + skinName.value, content_css: publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css', ...options, setup: (editor: Editor) => { console.log(editor, 'setup',); let mybutton = t('contract.Webcontractterm.mybutton') // 添加自定义按钮 数据库字段 editor.ui.registry.addButton('mybutton', { tooltip: mybutton, icon: 'table', // 使用内置的图标(例如:'bold'、'italic'等) onAction: function () { SqltextOpen.value = true; }, }); // editor.ui.registry.addButton('Buttonfile', { // // text: 'My Button', // 按钮的文本 // icon: '<svg width="16" height="16" fill="currentColor" class="bi bi-star" viewBox="0 0 16 16"><path d="M8 12.27l3.46 2.03-.93-3.56L14 6.89l-3.62-.03L8 2.5l-1.38 4.36L2 6.89l3.47 3.88-.93 3.56L8 12.27z"/></svg>', // 自定义 SVG 图标 // onAction: function () { // // 按钮点击后的操作 // editor.insertContent('<p>这是自定义按钮的内容!</p>'); // }, // }); editor.ui.registry.addButton('Buttonfileother', { // 数据库表名 // tooltip: t('contract.Webcontractterm.Buttonfileother'), icon: 'table', // 设置图标类名 onAction: function () { SqltableOpen.value = true; // 按钮点击后的操作 // editor.insertContent('<p>这是自定义按钮的内容!</p>'); }, }); // console.log(editor, 'ig'); editorRef.value = editor; editor.on('init', e => initSetup(e)); editor.on('NodeChange', () => { bindInputEventListeners(editor); // 重新绑定事件,确保动态元素被捕获 }); }, deprecation_warnings: false, placeholder, }; }); // 绑定输入框事件 function bindInputEventListeners(ed: any) { // 获取编辑器内所有 input/textarea 元素 const inputElements = ed.getDoc().querySelectorAll('input, textarea,td'); inputElements.forEach(input => { // 避免重复绑定事件 if (input['__eventBound']) return; // 监听输入事件(input/change) input.addEventListener('input', handleInputEvent); input.addEventListener('change', handleInputEvent); // 标记已绑定事件 input['__eventBound'] = true; }); } // 输入事件处理函数 function handleInputEvent(e: Event) { try { // as HTMLInputElement | HTMLTextAreaElement; const input: any = e.target; const editor = tinymce.activeEditor; // 获取当前编辑器实例 if (!editor) return; // 打印输入内容(可替换为业务逻辑) console.log('输入框值变化:', { id: input.id, name: input.name, value: input.value, eventType: e.type, }); const parser = new DOMParser(); const doc = parser.parseFromString(editor.getContent(), 'text/html'); // 查找所有 input 元素 const inputs:any = doc.querySelectorAll('input'); inputs.forEach(input1 => { // console.log(input.getAttribute('id')); if (input1.getAttribute('id') == input.id) { input.setAttribute('value', input.value); input1?.onfocus(); } }); // 触发 Vue 数据更新(示例) if (editor) { editor.setContent(editor.getContent()); // 强制更新编辑器内容,触发 v-model 同步 } } catch (error) { } } const disabled = computed(() => { const { options } = props; const getDisabled = (options && Reflect.get(options, 'readonly')) || attrs.disabled; const editor = unref(editorRef); if (editor) { editor.setMode(getDisabled ? 'readonly' : 'design'); } return !!getDisabled || false; }); watch( () => attrs.disabled, () => { const editor = unref(editorRef); if (!editor) { return; } editor.setMode(attrs.disabled ? 'readonly' : 'design'); }, ); watch( () => editorRef.value, () => { const editor = unref(editorRef); if (!editor) { return; } editor.setMode(attrs.disabled ? 'readonly' : 'design'); }, ); watch( () => appStore.getDarkMode, () => { updateEditor(); }, ); watch( () => props.placeholder, () => { debounceUpdateEditor(); }, ); onMountedOrActivated(() => { if (!initOptions.value.inline) { tinymceId.value = buildShortUUID('tiny-vue'); } nextTick(() => { setTimeout(() => { initEditor(); }, 30); }); }); onBeforeUnmount(() => { destroy(); }); onMounted(() => { }); onDeactivated(() => { destroy(); }); function destroy() { if (tinymce !== null) { tinymce?.remove?.(unref(initOptions).selector!); } } function initEditor() { const el = unref(elRef); if (el) { el.style.visibility = ''; } tinymce .init(unref(initOptions)) .then(editor => { emit('inited', editor); }) .catch(err => { emit('init-error', err); }); } function initSetup(e) { console.log(e, 'eeeeeee'); const editor = unref(editorRef); if (!editor) { return; } const value = props.value || ''; editor.setContent(value); bindModelHandlers(editor); bindHandlers(e, attrs, unref(editorRef)); } function setValue(editor: Recordable, val: string, prevVal?: string) { if (editor && typeof val === 'string' && val !== prevVal && val !== editor.getContent({ format: attrs.outputFormat })) { editor.setContent(val); } } function bindModelHandlers(editor: any) { const modelEvents = attrs.modelEvents ? attrs.modelEvents : null; const normalizedEvents = Array.isArray(modelEvents) ? modelEvents.join(' ') : modelEvents; watch( () => props.value, (val: string, prevVal: string) => { setValue(editor, val || '', prevVal); }, { immediate: true, }, ); editor.on(normalizedEvents ? normalizedEvents : 'change keyup undo redo', () => { const content = editor.getContent({ format: attrs.outputFormat }); emit('update:value', content); emit('change', content); }); editor.on('FullscreenStateChanged', e => { fullscreen.value = e.state; }); } function handleInsertImg(url: string) { const editor = unref(editorRef); if (!editor || !url) { return; } editor.execCommand('mceInsertContent', false, `<img src="${url}"/>`); const content = editor?.getContent() ?? ''; setValue(editor, content); } function handleImageUploading(name: string) { const editor = unref(editorRef); if (!editor) { return; } editor.execCommand('mceInsertContent', false, getUploadingImgName(name)); const content = editor?.getContent() ?? ''; setValue(editor, content); } function handleDone(name: string, url: string) { const editor = unref(editorRef); if (!editor) { return; } const content = editor?.getContent() ?? ''; const val = content?.replace(getUploadingImgName(name), `<img src="${url}"/>`) ?? ''; setValue(editor, val); } function getUploadingImgName(name: string) { return `[uploading:${name}]`; } function updateEditor() { destroy(); initEditor(); } function CloseSqltext(e: Boolean, data: any, checkdata: any) { // console.log(tinymceId.value, document.getElementById(tinymceId.value).focus()) // console.log(tinymce, editorRef.value, editorRef.value.selection,) // setValueAll(editorRef.value.getContent(), data) SqltextOpen.value = false; try { if (e) { // const editor = elRef.value.editor editorRef.value?.insertContent(data); console.log(editorRef.value?.getContent()); // props.value = editorRef.value?.getContent() emit('update:value', editorRef.value?.getContent()); emit('change', editorRef.value?.getContent()); emit('checkitemeditor', editorRef.value?.getContent()); } } catch (error) { // console.error('handleInsertImg error:', error); } } function CloseSqltable(e: Boolean, data: any, checkdata: any) { SqltableOpen.value = false; if (e) { editorRef.value?.insertContent(data); // props.value = editorRef.value?.getContent() emit('update:value', editorRef.value?.getContent()); emit('change', editorRef.value?.getContent()); emit('checkitemeditor', checkdata); } } return { prefixCls, containerWidth, initOptions, tinymceContent, elRef, tinymceId, handleInsertImg, handleImageUploading, handleDone, editorRef, fullscreen, disabled, SqltextOpen, CloseSqltext, SqltableOpen, CloseSqltable, business, }; }, }); </script> <style lang="less" scoped></style> <style lang="less"> @prefix-cls: ~'@{namespace}-tinymce-container'; .@{prefix-cls} { position: relative; line-height: normal; textarea { z-index: -1; visibility: hidden; } } /* 定义图标样式 */ .tox-tbtn .tox-icon.tox-icon--my-icon { background-image: url('https://pic4.zhimg.com/v2-4d9e9f936b9968f53be22b594aafa74f_r.jpg'); background-size: contain; background-repeat: no-repeat; background-position: center; } </style> tooltip: mybutton mybutton代码没有执行
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值