You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iMacros批量替换HTML图片src属性的技术求助

How to Replace All Image Sources in a Specific Div with iMacros

Got it, let's figure out how to update those image sources using iMacros! There are two solid approaches depending on whether you know the exact number of images in the outerd3 div or need a flexible, dynamic solution.

Approach 1: Fixed Number of Images (3 in your case)

If you're certain there will always be 3 images inside the div, this straightforward script will get the job done quickly:

VERSION BUILD=1002 RECORDER=CR
' Replace src of the first image in outerd3
TAG POS=1 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg")
' Replace src of the second image
TAG POS=2 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg")
' Replace src of the third image
TAG POS=3 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg")

Each TAG line targets an image by its position within the outerd3 div, then uses SETATTRIBUTE to overwrite the src value to avatar.jpg. Super simple and reliable for fixed counts.

Approach 2: Dynamic Solution (Works for Any Number of Images)

If the number of images might change later, use this script that automatically counts and updates all images in the div:

VERSION BUILD=1002 RECORDER=CR
SET !ERRORIGNORE YES
SET !LOOP 1

' Extract the HTML of the outerd3 div and count how many img tags it has
TAG POS=1 TYPE=DIV ATTR=ID:outerd3 EXTRACT=HTM
SET imgCount EVAL("var html = '{{!EXTRACT}}'; html.match(/<img/g) ? html.match(/<img/g).length : 0;")

' Loop through each image and update its src
TAG POS={{!LOOP}} TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg")

' Stop the loop once all images are processed
IF {{!LOOP}} EQUAL {{imgCount}} SET !LOOP {{imgCount}}

Here's a quick breakdown of how this works:

  • EXTRACT=HTM pulls the full HTML content of the outerd3 div.
  • The EVAL command uses a regex match to count how many <img> tags are present in that HTML.
  • The loop runs once for each image, updating the src attribute every time.
  • The final IF statement ensures the loop stops after processing all images, so you don't waste extra iterations.

Just tweak the VERSION line to match your iMacros build (this example uses the Chrome version), then run the script—your images will all point to avatar.jpg in no time!

内容的提问来源于stack exchange,提问作者CherylAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:31:51