Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bind spm text #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="hello" data-spma="aa">
<span>show spm:{{spmText}}</span>
<div class="hello" data-spma="aa" @click="updateSpmText">
<span>show spm:{{ spmText }}</span>
<div data-spmb="bb">
<button data-spmc="cc">Click it</button>
</div>
Expand All @@ -14,12 +14,36 @@
// TODO 利用事件代理实现一个简单的收集spm信息的方法,注意不是针对每一个按钮进行函数绑定。场景:考虑一下如果一个页面中有很多按钮,需要如何处理
export default {
name: 'HelloWorld',
data: ()=>{
data: () => {
return {
spmText: 'xx.xx.xx'
}
}
}
spmText: 'xx.xx.xx',
};
},
methods: {
updateSpmText(e) {
const buttonNode = e.target;
if (e.srcElement.nodeName !== 'BUTTON') return;
const spma = this.getSpmValue('data-spma', buttonNode);
const spmb = this.getSpmValue('data-spmb', buttonNode);
const spmc = buttonNode.getAttribute('data-spmc');
this.spmText = [spma, spmb, spmc].join('.');
},
getSpmValue(attributeName, buttonNode) {
const nodeList = document.querySelectorAll('[' + attributeName + ']');
if (nodeList.length > 0) {
let index = -1;
nodeList.forEach((node, i) => {
if (node.contains(buttonNode)) {
index = i;
}
});
if (index > -1)
return nodeList[index].getAttribute(attributeName) || '';
}
return '';
},
},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
Expand Down