简体   繁体   English

对将 JS 变量插入 Jquery 感到困惑

[英]Confused about inserting a JS variable into Jquery

I'd like to scroll to a certain row in my HTML table, which contains dates in the format of我想滚动到 HTML 表中的某一行,其中包含格式为

Nov-17-2021

Currently, I have this date.js function which gets today's date and inputs it in a <span>目前,我有这个 date.js function 获取今天的日期并将其输入到<span>

const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];

const d = new Date();

var today = new Date();

var date = today.getDate() + '-' + monthNames[d.getMonth()] + '-' + today.getFullYear();

document.getElementById("date").innerHTML = date;

I found that I can use JQuery to scroll easily to some text in a page with我发现我可以使用 JQuery 轻松滚动到页面中的某些文本

function anotherFunction() {
    $(window).scrollTop($("table:contains('some text'):last").offset().top);
}

How can I use the variable date from my Javascript code to scroll to this text in my table, using JQuery?如何使用 Javascript 代码中的变量date滚动到表格中的此文本,使用 JQuery? Essentially, I'd like a button that jumps to today's date in my table.本质上,我想要一个按钮,可以在我的表中跳转到今天的日期。

I'm pretty sure there are better ways to do this also, but I am an absolute beginner in programming and I don't know much, so I'm sorry for being very blunt and thank you!我很确定也有更好的方法可以做到这一点,但我是编程的绝对初学者,我知道的不多,所以很抱歉非常直率,谢谢!

Have you tried using including the date in your string?您是否尝试过在字符串中包含日期?

function anotherFunction() {
    date = document.getElementById("date").innerHTML
    $(window).scrollTop($(`table:contains(${date}):last`).offset().top);
}

You can add the date string using string concatenation or string literals.您可以使用字符串连接或字符串文字添加日期字符串。

"table:contains(" + date +"):last"
`table:contains(${date}):last`

Example:例子:

 // Fix a date for demo var date = "18-Dec-2021" $("button").click(() => { $(window).scrollTop($("tr:contains('" + date + "'):last").offset().top); });
 td { height: 200px; border: 1px solid #CCC; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>click me</button> <table> <tbody> <tr><td>01-Dec-2021</td></tr> <tr><td>10-Dec-2021</td></tr> <tr><td>18-Dec-2021</td></tr> <tr><td>31-Dec-2021</td></tr> </tbody> </table>

Your function anotherFunction is used to scroll to to an element inside a table having text 'some text' .您的 function anotherFunction用于滚动到具有文本'some text'的表格内的元素。

Only what you have to do is, just trigger the function anotherFunction with some dynamic paramater for the text.只有你需要做的是,只需触发 function anotherFunction并带有一些文本的动态参数。

Below is how its done.下面是它是如何完成的。

 function firstFunction() { const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; const d = new Date(); var today = new Date(); var date = today.getDate() + '-' + monthNames[d.getMonth()] + '-' + today.getFullYear(); document.getElementById("date").innerHTML = date; anotherFunction(date) } function anotherFunction(text) { $(window).scrollTop($(`table:contains(${text}):last`).offset().top); }
 table { margin-bottom: 100vh; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <table> <tr> <th>Scroll to bottom and click the button</th> </tr> <tr> <td id="date">Your Date Will Appear here</td> </tr> </table> <button onclick="firstFunction()">Call First Function</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM