【发布时间】:2022-01-24 06:13:59
【问题描述】:
我有不同的自定义形状,当我单击按钮时,应将一张图像附加到其具有相同形状的父 SVG 文件(在 svg 形状内屏蔽图像)。
除了以下故障之外,一切都按预期工作
- 尝试将我的 svg 路径包裹在
<clipPath>中,但得到<clippath> - 试图添加
<image>标签,但它会自动转换为<img>标签。
如何将元素附加为区分大小写和精确的元素。
HTML
<svg id="cusotmShape" version="1.1" style="width:100px;height:100px;" stroke="off" set-as-background="no" class="custom-shape cs-shape" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 135.8 130.8" xml:space="preserve">
<path rp-cp-id="rp-shape-02" class="cs-element-layer" fill="#000000" style="stroke:transparent;stroke-width:0px;" d="M100.4,128.4l-26.8-13.5c-2.9-1.5-6.4-1.4-9.3,0.2l-26.2,14.4c-7.3,4-15.9-2.1-14.7-10.3l4.5-29.6 c0.5-3.2-0.6-6.5-3-8.8L3.1,60.3c-6-5.7-2.9-15.8,5.2-17.1l29.6-4.8c3.2-0.5,6-2.6,7.4-5.6L58.1,5.7C61.6-1.8,72.2-2,76,5.4L89.7,32 c1.5,2.9,4.4,4.9,7.6,5.3l29.7,3.8c8.2,1,11.7,11,5.8,16.9l-20.9,21.3c-2.3,2.3-3.3,5.7-2.7,8.9l5.6,29.5 C116.3,125.7,107.8,132.1,100.4,128.4z"></path>
</svg>
<div><button type="button" id="insertImage">Insert Image</button></div>
脚本
jQuery(document).on('click', '#insertImage', function (e) {
var _custom_shape_svg = jQuery('svg#cusotmShape');
var _custom_shape_svg_id = jQuery('svg#cusotmShape path').attr('rp-cp-id')
_custom_shape_svg.wrapInner('<clipPath id="' + _custom_shape_svg_id +'"></clipPath>');
_custom_shape_svg.append('<image clip-path="url(#' + _custom_shape_svg_id +')" xlink:href="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" src="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" alt="Image" height="100" width="100" class="svg__image" />');
});
我得到了什么:
<svg id="cusotmShape" version="1.1" style="width:100px;height:100px;" stroke="off" set-as-background="no" class="custom-shape cs-shape" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 135.8 130.8" xml:space="preserve">
<clippath id="rp-shape-02">
<path rp-cp-id="rp-shape-02" class="cs-element-layer" fill="#000000" style="stroke:transparent;stroke-width:0px;" d="M100.4,128.4l-26.8-13.5c-2.9-1.5-6.4-1.4-9.3,0.2l-26.2,14.4c-7.3,4-15.9-2.1-14.7-10.3l4.5-29.6 c0.5-3.2-0.6-6.5-3-8.8L3.1,60.3c-6-5.7-2.9-15.8,5.2-17.1l29.6-4.8c3.2-0.5,6-2.6,7.4-5.6L58.1,5.7C61.6-1.8,72.2-2,76,5.4L89.7,32 c1.5,2.9,4.4,4.9,7.6,5.3l29.7,3.8c8.2,1,11.7,11,5.8,16.9l-20.9,21.3c-2.3,2.3-3.3,5.7-2.7,8.9l5.6,29.5 C116.3,125.7,107.8,132.1,100.4,128.4z"></path>
</clippath>
<img clip-path="url(#rp-shape-02)" xlink:href="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" src="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" alt="Image" height="100" width="100" class="svg__image">
</svg>
预期
<svg id="cusotmShape" version="1.1" style="width:100px;height:100px;" stroke="off" set-as-background="no" class="custom-shape cs-shape" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 135.8 130.8" xml:space="preserve">
<clipPath id="rp-shape-02">
<path rp-cp-id="rp-shape-02" class="cs-element-layer" fill="#000000" style="stroke:transparent;stroke-width:0px;" d="M100.4,128.4l-26.8-13.5c-2.9-1.5-6.4-1.4-9.3,0.2l-26.2,14.4c-7.3,4-15.9-2.1-14.7-10.3l4.5-29.6 c0.5-3.2-0.6-6.5-3-8.8L3.1,60.3c-6-5.7-2.9-15.8,5.2-17.1l29.6-4.8c3.2-0.5,6-2.6,7.4-5.6L58.1,5.7C61.6-1.8,72.2-2,76,5.4L89.7,32 c1.5,2.9,4.4,4.9,7.6,5.3l29.7,3.8c8.2,1,11.7,11,5.8,16.9l-20.9,21.3c-2.3,2.3-3.3,5.7-2.7,8.9l5.6,29.5 C116.3,125.7,107.8,132.1,100.4,128.4z"></path>
</clipPath>
<image clip-path="url(#rp-shape-02)" xlink:href="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" src="https://www.vzhurudolu.cz/assets/codepen/kubik_ig.jpg" alt="Image" height="100" width="100" class="svg__image">
</svg>
【问题讨论】:
标签: jquery