[英]How to share dynamic text content of DIV or SPAN on whatsapp using jquery?
我一直在嘗試在whatsapp上分享 DIV 的文本內容。 但是,還沒有成功。 我不想分享整個文檔。 我只想分享其中的一部分(我只想分享showques
的內容)。
這是我的代碼:
<body>
<div id="showques" style="font-size: 48px;">
</div>
<div id="showans" style="font-size: 48px;">
</div>
<a href="whatsapp://send?text=#showall" data-action="share/whatsapp/share"
tyle="background-color: green; color: ivory;font-size: 48px;height:48px;width:500px">SHARE IN WHATSAPP</a>
showques
的值從以下代碼更新:
<script>
function nxtRiddle() {
var chooserdl = [];
for (i = 0; i < 164; i++) {
chooserdl.push(i);
}
console.log(chooserdl);
var riddleno = chooserdl[Math.floor(Math.random() * chooserdl.length)];
console.log(riddleno);
$('#showques').append(riddleques[riddleno]);
$('#showans').append(riddleans[riddleno]);
return;
}
nxtRiddle(0);
</script>
這是我從 webflow 獲得的代碼。 有用!
將此粘貼到頁面的內部 <head> 標記中:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
將此粘貼到 <body> 標記中:
<div class='divblock'>
This is the first line.
This is the second line.
</div>
<button id='button'> WHATSAPP </button>
將此粘貼在 < /body > 標記之前:
<script type="text/javascript">
(function clickMe() {
const button = document.getElementById("button");
var divblock = $('.divblock').text();
button.addEventListener("click", event => {
// Whatsapp Message on Button Click
window.open("https://api.whatsapp.com/send?text=Show div text: " + divblock)
});
})();
</script>
Output:
顯示 div 文本:這是第一行。 這是第二行。
首先,您需要從DOM
獲取問題的內容。 然后,您必須從 JS 觸發鏈接單擊事件,並使用正確的 WhatsApp url
打開一個新選項卡。 希望這會有所幫助。
document.getElementById("whatsAppShareButton").addEventListener('click', function(e) { e.preventDefault(); var question = encodeURIComponent(document.getElementById("showques").textContent); window.open('whatsapp://send?text=' + question, '_blank'); });
<div id="showques" style="font-size: 48px;"> This is a sample question... </div> <div id="showans" style="font-size: 48px;"> </div> <a href="#" id="whatsAppShareButton" data-action="share/whatsapp/share" style="background-color: green; color: ivory;font-size: 48px;height:48px;width:500px">SHARE IN WHATSAPP</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.