![]() | 1 summerwar 2022-12-10 09:09:15 +08:00 如果是提交的表单,你重写 submit ,在里面替换 如果是展示的,在模板里写 replace 就好了 不需要用 js |
![]() | 2 summerwar 2022-12-10 09:09:40 +08:00 需要用 js ,但是不需要用 js 判断 id 来改变 input 中的 value |
![]() | 3 molvqingtai 2022-12-10 09:12:27 +08:00 via Android const input = document.querySelector("image-link3") if input.value === xxx input.value = aaa 手机不好打,大概这样子 |
![]() | 4 SkyRing OP @molvqingtai 我试试看 |
![]() | 5 SkyRing OP  感觉似乎并无软用啊 |
![]() | 6 SkyRing OP |
![]() | 7 xlsama 2022-12-10 09:34:58 +08:00 const el = document.querySelector('#image-link3'); setTimeout(() => { el.setAttribute('id', 'xxx'); }, 2000); const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.type === 'attributes') { console.log('attributes changed'); el.setAttribute('value', 'https://a.aaa.net/Rg1xQrV.png'); } }); }); observer.observe(el, { attributeFilter: ['id'], }); |
![]() | 10 tonyrft 2022-12-10 09:41:43 +08:00 嗯,您想要只更改 URL 的一部分而不是整个 URL ,这是可以实现的。您可以使用 Javascript 的 String.replace() 方法来执行替换操作,该方法允许您指定要替换的字符串和替换为的字符串。 例如,您可以使用以下代码来执行替换操作: var inputElement = document.getElementById('image-link3'); inputElement.value = inputElement.value.replace('https://i.xxx.com', 'https://a.aaa.net'); 请注意,我们在上面的代码中使用了两个参数的 replace() 方法,第一个参数是要替换的字符串(即 'https://i.xxx.com'),第二个参数是替换为的字符串(即 'https://a.aaa.net')。 如果您需要在页面加载完成后执行此操作,可以将上述代码放在 window.onload 或 document.ready 事件的回调函数中,如前面所述。 希望这些信息对您有帮助。 |
![]() | 11 xlsama 2022-12-10 09:53:31 +08:00 |
![]() | 12 xlsama 2022-12-10 09:55:11 +08:00 el.setAttribute('value', el.value.replace(/http(s):\/\/(.*)\//, 'https://a.aaa.net/')); |
![]() | 13 SkyRing OP @tonyrft 感谢大佬,非常有用,不过又遇到了个新问题, ![]() |
![]() | 14 lxiian 2022-12-10 12:44:12 +08:00 via iPhone 你问问 chatgpt ,楼上那个好像就是 chatgpt 给的答案 |
![]() | 16 xiangyuecn 2022-12-10 13:30:31 +08:00 ![]() 面向问答编程。不建议提供任何直接答案。 |
![]() | 17 15855pm 2022-12-10 13:48:21 +08:00 过于伸手党了,这不是 js 的问题,你需要先去学习正则表达式,太基础的东西还是问搜索引擎吧 |